Service Provider

Service provider is responsible for registration services in the dependency injection.

Register a service

To register a service, that will be accessible via dependency injection, just create a class named in the following format NameOfServiceServiceProvider and put it into app/services directory.
During the bootstrap process, all service providers are registered in the dependency injection container. Service provider loader generates file services.php inside app/config directory, which contains the list of available services in the application. In development environment, that file is generated on every request. In case of production environment loader use already generated file to setups services.

Service provider must implements Vegas\DI\ServiceProviderInterface and must have constant const SERVICE_NAME that contains name of service.
The register method registers service in the dependency injection container. Method getDependencies must returns an array (also empty array) with list of depencened services, for example, scaffolding requires Mongo and CollectionManager service.

Examples

Scaffolding Service Provider

In the following example we register a scaffolding service:

use Phalcon\DiInterface;
use Vegas\DI\ServiceProviderInterface;
use Vegas\DI\Scaffolding;

class ScaffoldingServiceProvider implements ServiceProviderInterface
{
    const SERVICE_NAME = 'scaffolding';

    /**
     * {@inheritdoc}
     */
    public function register(DiInterface $di)
    {
        $adapter = new Scaffolding\Adapter\Mongo();
        $di->set(self::SERVICE_NAME, new Scaffolding($adapter), true);
    }

    public function getDependencies()
    {
        return array(
            MongoServiceProvider::SERVICE_NAME,
            CollectionManagerServiceProvider::SERVICE_NAME
        );
    }
}

I18n Service Provider

In example below you can see Gettext based i18n service provider. For more informations about i18n adapters available in Phalcon, check Phalcon Incubator .

use Phalcon\DiInterface;
use Vegas\DI\ServiceProviderInterface;

class I18nServiceProvider implements ServiceProviderInterface
{
    const SERVICE_NAME = 'i18n';

    /**
     * {@inheritdoc}
     */
    public function register(DiInterface $di)
    {
        $config = $di->get('config');
        $di->set('i18n', function() use ($config) {
            return new \Vegas\Translate\Adapter\Gettext(array(
                'locale' => $config->application->language,
                'file' => 'messages',
                'directory' => APP_ROOT.'/lang'
            ));
        });
    }

    /**
     * {@inheritdoc}
     */
    public function getDependencies()
    {
        return array();
    }
}

Assets Manager Service Provider

In the following example we register default Vegas CMF assets manager service:

use Phalcon\DiInterface;
use Vegas\DI\ServiceProviderInterface;

/**
 * Class AssetsServiceProvider
 */
class AssetsServiceProvider implements ServiceProviderInterface
{
    const SERVICE_NAME = 'assets';

    /**
     * {@inheritdoc}
     */
    public function register(DiInterface $di)
    {
        $di->set(self::SERVICE_NAME, '\Vegas\Assets\Manager', true);
    }

    public function getDependencies()
    {
        return array();
    }
}