TYPO3 的命名空间

简介

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}
  • 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.
$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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值