TYPO3 的命名空间

来源:互联网 发布:java静态变量重新赋值 编辑:程序博客网 时间:2024/04/29 08:23

简介

TYPO3 核心从 beta1 版本开始使用了命名空间。为保持扩展的兼容性,放置了一个兼容层。使用 t3lib_div 方式调用核心类仍然正常工作,但是重定向到新的\TYPO3\CMS\Core\Utility\GeneralUtility 类。作为扩展开发者,强烈建议你从现在开始使用命名空间的类。TYPO3 版本 6.2 装将会移除兼容层。

基于此系统架构改变, TYPO3 新增了三个系统扩展。 它们把核心分成三个部分。

  • core 包含基础类,如: Bootstrap, Logging Functions, Mail- and Caching handling
  • frontend 包含诸如 typoscript 处理和渲染等函数
  • backend 包含 TCEmain, Forms and Modules

The namespace change levels the path for the next features to introduce in further TYPO3 versions like composer support (seehttp://getcomposer.org/doc/00-intro.md) and FLOW3 integration.

实现细节

To provide the mentioned compat layer not only all most files in the core were moved to new locations and renamed according the streamlined naming schema, but the old files are kept, too. They are still in the known places, but contain as only content a require to their namespaced successor. An adopted class loader makes use of the class_alias() function to create a language level mapping from new to old classnames. The old class names are still available although php uses the new ones physically.

Extensions may use the mechanism for own class name changes. It has to be implemented like in typo3/sysext/core/Migrations/Code/ClassAliasMap.php for example.

Does and don'ts for developers

  • Structure of a namespaced class name: {VendorName}\{PackageName}\({CategoryName}\)*{ClassName}
  • New namespace, class, interface, exception, method, variable, constant and file names are subject to the FLOW3 CGLhttp://flow.typo3.org/documentation/codingguidelines/php.html#naming
  • The TYPO3 core uses \TYPO3\CMS as vendor name. As a extension developer you are not allowed to use this vendor in your own extension! Instead, use your company name. Until now there is no vendor registration like it was there for extension keys, so ensure to pick a unique vendor name. Your vendor must have only one part for now, not two as the core vendor. Valid vendor names for extensions could be \Enet or \DKD for example. Please check the web/github quickly to ensure uniqueness. Domain and brand names are also good vendor names (without dots and tlds).
  • The second part of the namespace (in case of core classes the third part) is treated as the extension key. Pick an extension key to your liking, thanks to the unique vendor name collisions with other extensions will not happen. The extension key part in the namespace have to be written in UpperCamelCase and is internally transformed to lowercase_underscore.
  • Further parts of the namespace - if used - have to describe a category the class belongs to. While browsing through the class tree the category name should give you a good impression of the domain of the belonging classes.
  • Class names used in strings must always be fully qualified and noted with escaped backslashes to avoid errors (seehttp://www.php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.quote). As they are always fully qualified the leading backslashes have to be omitted. For Example use
$configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
  • The use of include() or require() in extensions is no longer the way to go. Instead you should make use of the autoloader functionality. Create a file named ext_autoload.php in your extension and put all required files in. They will be available then without further action.
  • When you want to use class names in classes sharing the same namespacce, you may NOT use the short name, but always the full qualified version:
namespace Extbase\BlogExample\Domain\Model;class Post extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {[...]public function removeTag(\Extbase\BlogExample\Domain\Model\Tag $tag) {    $this->tags->detach($tag);}[...]}

the same goes for the definition of an objectStorage to use, you must give the full qualified name:

/** * @var \TYPO3\CMS\Extbase\Persistence\Generic\ObjectStorage<\Extbase\BlogExample\Domain\Model\Tag> */protected $tags;

Extbase

Backend Modules

To use namespaces in backend modules you need to prefix the extension key with your vendor namespace when registering the module (in ext_tables.php):

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule(    '<vendorName>.' . $_EXTKEY,    ...);

Frontend Plugins

To use namespaces in frontend plugins you need to prefix the extension key with your vendor namespace when configuring the module (in ext_localconf.php):

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(    '<vendorName>.' . $_EXTKEY,    ...);
  Note Replace the <vendorName> with your own vendor, do not use dots or tlds inside. Don't forget the dot inside the string, it is used as delimiter

Compatibility

  • Hint about ext_localconf and ext_tables that requires are evil and should be avoided in general (and especially at this point). This especially gives issues with the php < 5.3.7 compat layer.
原创粉丝点击