<?php
/**
* cvServiceContainerDefinitions
*
* Contains definitions of all the services a specific container
*
* @package CYBERVILLE
* @subpackage DI
* @author Fabien Potencier <[email protected]>
* Kochergin Nick <[email protected]>, <http://cyberville-project.ru>
* @version $Id$
*/
class cvServiceContainerDefinitions extends cvServiceContainer {
protected $definitions = array();
protected $aliases = array();
/**
* Sets an alias for an existing service.
*
* @param string $alias The alias to create
* @param string $id The service to alias
* @param string|false $subcontainer Subcontainer Service
*/
public function setAlias($alias, $id, $subcontainer = false) {
$this->aliases[$alias] = array('service' => $id, 'container' => $subcontainer);
}
/**
* Gets all defined aliases.
*
* @return array An array of aliases
*/
public function getAliases() {
return $this->aliases;
}
/**
* Registers a service definition.
*
* This methods allows for simple registration of service definition
* with a fluid interface.
*
* @param string $id The service identifier
* @param string $class The service class
*
* @return cvServiceDefinition A cvServiceDefinition instance
*/
public function register($id, $class) {
return $this->setServiceDefinition($id, new cvServiceDefinition($class));
}
/**
* Adds the service definitions.
*
* @param array $definitions An array of service definitions
*/
public function addServiceDefinitions(array $definitions) {
foreach ($definitions as $id => $definition) {
$this->setServiceDefinition($id, $definition);
}
}
/**
* Sets the service definitions.
*
* @param array $definitions An array of service definitions
*/
public function setServiceDefinitions(array $definitions) {
$this->definitions = array();
$this->addServiceDefinitions($definitions);
}
/**
* Gets all service definitions.
*
* @return array An array of cvServiceDefinition instances
*/
public function getServiceDefinitions() {
return $this->definitions;
}
/**
* Sets a service definition.
*
* @param string $id The service identifier
* @param cvServiceDefinition $definition A cvServiceDefinition instance
*
* @return cvServiceDefinition
*/
public function setServiceDefinition($id, cvServiceDefinition $definition) {
return $this->definitions[$id] = $definition;
}
/**
* Returns true if a service definition exists under the given identifier.
*
* @param string $id The service identifier
*
* @return Boolean true if the service definition exists, false otherwise
*/
public function hasServiceDefinition($id) {
return array_key_exists($id, $this->definitions);
}
/**
* Gets a service definition.
*
* @param string $id The service identifier
*
* @return cvServiceDefinition A cvServiceDefinition instance
*/
public function getServiceDefinition($id) {
if (!$this->hasServiceDefinition($id)) {
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
}
return $this->definitions[$id];
}
/**
* Replaces parameter placeholders (%name%) by their values.
*
* @param mixed $value A value
*
* @return mixed The same value with all placeholders replaced by their values
*/
public function resolveValue($value) {
if (is_array($value)) {
$args = array();
foreach ($value as $k => $v) {
$args[$this->resolveValue($k)] = $this->resolveValue($v);
}
$value = $args;
} else
if (is_string($value)) {
if (preg_match('/^%([^%]+)%$/', $value, $match)) {
// we do this to deal with non string values (boolean, integer, ...)
// the preg_replace_callback converts them to strings
if (!$this->hasParameter($name = strtolower($match[1]))) {
throw new RuntimeException(sprintf('The parameter "%s" must be defined.', $name));
}
$value = $this->getParameter($name);
} else {
$value = str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\1/', array($this, 'replaceParameter'), $value));
}
}
return $value;
}
protected function replaceParameter($match) {
if (!$this->hasParameter($name = strtolower($match[2]))) {
throw new RuntimeException(sprintf('The parameter "%s" must be defined.', $name));
}
return $this->getParameter($name);
}
}
?>