菜单

TYPO3 Services

2011年03月22日 - extensions

With a services extension you can extend the functionality of TYPO3 (or an extension which uses services), without any changes to the original code of TYPO3 or the extension.

Services are PHP classes inside of an extension similar to FE-plugins. Usually when you use a class, you address it directly by creating an instance:

require_once(t3lib_extMgm::extPath('some_extension').'class.tx_some_extension_class.php');
$obj = t3lib_div::makeInstance('tx_some_extension_class');

Using a service class will be done by a function which chooses the right service automatically by passing the service type name (not the class name):

$serviceObj = t3lib_div::makeInstanceService('my_service_type');

The difference is that the class name itself and its usage is not hardcoded. The same service can be provided by different extensions. The service with the highest priority and quality is chosen automatically.

Two reasons to use Services

1. Freedom of implementation

A service might be implemented multiple times because of different environments like operating systems (Unix, Windows), available PHP extensions or other dependencies like Perl.

An example could be a service extension depending on a Perl script which gives very good results. Another extension can implement the same service by using pure PHP. This implementation might not give as good results (because of a less good implementation) as the Perl solution but it makes the service available where Perl is not available.

2. Extend functionality with extensions

Services are able to handle service subtypes. This means, a service of the type ‘fileMeta” which extracts meta data from files, provides information for which file type the service is implemented.

if (is_object($serviceObj = t3lib_div::makeInstanceService('fileMeta', $fileExtension))) {
$meta =  serviceObj->getFileMeta($filename);
}

This way the code don’t has to be changed for new file types. An extension can implement a fileMeta service for the file type ‘mp3’ and it will be used automatically by code that uses the fileMeta service.

 

ext_localconf.php

In ext_localconf.php the services will be registered to the system which looks like this:

t3lib_extMgm::addService($_EXTKEY, 'textExtract' /* sv type */, 'tx_cctxtextexec_sv1' /* sv key */,
array(
'title' => 'Text extraction for pdf',
'description' => 'This service depends on pdftotext',
'subtype' => 'pdf',
'available' => true,
'priority' => 50,
'quality' => 50,
'os' => '',
'exec' => 'pdftotext',
'classFile' => t3lib_extMgm::extPath($_EXTKEY).'sv1/class.tx_cctxtextexec_sv1.php',
'className' => 'tx_cctxtextexec_sv1',
)
);

You might notice that the array contains mainly information you create in the kickstarter.

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注