Просмотр файла cvServiceContainer/cvServiceContainerLoader.php

Размер файла: 2.6Kb
<?php

/**
 * cvServiceContainerLoader
 * 
 * cvServiceContainerLoader is the abstract class used by all built-in loaders.
 * 
 * @package CYBERVILLE
 * @subpackage DI
 * @author Fabien Potencier <[email protected]>
 *         Kochergin Nick <[email protected]>, <http://cyberville-project.ru>
 * @version $Id$
 */
abstract class cvServiceContainerLoader implements cvServiceContainerLoaderInterface {
    protected $container;

    /**
     * Constructor.
     *
     * @param cvServiceContainerDefinitions $container A cvServiceContainerDefinitions instance
     */
    public function __construct(cvServiceContainerDefinitions $container = null) {
        $this->container = $container;
    }

    /**
     * Sets the service container of definitions attached to this loader.
     *
     * @param cvServiceContainerDefinitions $container A cvServiceContainerDefinitions instance
     */
    public function setServiceContainer(cvServiceContainerDefinitions $container) {
        $this->container = $container;
    }

    /**
     * Loads a resource.
     *
     * A resource can be anything that can be converted to an array of
     * definitions and parameters by the doLoad() method.
     *
     * @param mixed $resource The resource path
     */
    public function load($resource) {
        if (!$this->container) {
            throw new LogicException('You must attach the loader to a service container.');
        }

        $resources = func_get_args();
        foreach ($resources as $resource) {
            list($definitions, $parameters) = $this->doLoad($resource);

            foreach ($definitions as $id => $definition) {
                if (is_array($definition)) {
                    $this->container->setAlias($id, $definition[0], $definition[1]);
                } else {
                    $this->container->setServiceDefinition($id, $definition);
                }
            }

            $currentParameters = $this->container->getParameters();
            foreach ($parameters as $key => $value) {
                $this->container->setParameter($key, $this->container->resolveValue($value));
            }
            $this->container->addParameters($currentParameters);
        }
    }

    /**
     * Loads a resource.
     *
     * Concrete classes implements this method to convert
     * the resource to an array of definitions and parameters.
     *
     * @param  mixed $resource The resource path
     *
     * @return array An array of definitions and parameters
     */
    abstract public function doLoad($resource);
}

?>