Service Locator

Introduction

Within in the core Phile uses a lot of services, which accessible through the ServiceLocator class. This makes it possible to replace a service with a complete new logic. In the following section, we tell you, which service classes available and how to replace a service with your own.

To register a service use:

\Phile\Core\ServiceLocator::registerService(
    <service name>,
    <class instance>
);

Each service has to implement an interface defined in Phile\ServiceLocator\<Service>Interface.

Service: Phile_Parser

You can replace the markdown parser with your own parser, if you like. This works by replacing the default parser with a plugin. To show you, check out the Markdown plugin that comes with Phile. Or, take a look at the Sundown-Parser-Plugin.

To replace the parser, you need only one line of code to register a new parser:

ServiceLocator::registerService('Phile_Parser', new \Phile\Parser\Sundown($this->settings));

Service: Phile_Template

The template engine is also a service, you can replace the default template engine (twig) with your own. The way you can do this, is the same like the Phile_Parser:

ServiceLocator::registerService('Phile_Template', new \Phile\Template\MyTemplateEngine());

Service: Phile_Cache

Internal PhileCMS use an object cache to improve the performance. however if you want another cache system, register your own:

ServiceLocator::registerService('Phile_Cache', new \Phile\Cache\MyCacheEngine());

Service: Phile_Data_Persistence

Phile has a simple file based data storage engine. Use it to persist some data, without a database. Here are some short examples how to use it:

$storage = \Phile\Core\ServiceLocator::getService('Phile_Data_Persistence');

// check first if a key exist before you call `delete` or `get`, else an exception can be thrown.
$storage->has('MyDataRow_123');

// set data to the storage
$storage->set('MyDataRow_123', $data);

// get data from the storage
$storage->get('MyDataRow_123');

// and delete data from the storage
$storage->delete('MyDataRow_123');

You can implement your own or maybe found a plugin with another storage engine, replace it like this:

ServiceLocator::registerService(
    'Phile_Data_Persistence',
    new \Phile\Persistence\MyPersistenceEngine()
);

Service: Phile_Parser_Meta

You can override the meta data parser with your own. The default parser handles meta in either a HTML block comment or a Javascript/PHP block comment. If you wanted to support YAML, this would be the service you would override.

Edit the source page on github…