Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/config/sfServiceConfigHandler.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function execute($configFiles)
{
$class = sfConfig::get('sf_app').'_'.sfConfig::get('sf_environment').'ServiceContainer';

$serviceContainerBuilder = new sfServiceContainerBuilder(sfConfig::getAll());
$serviceContainerBuilder = new sfServiceContainerBuilder();

$loader = new sfServiceContainerLoaderArray($serviceContainerBuilder);
$loader->load(self::getConfiguration($configFiles));
Expand Down
153 changes: 9 additions & 144 deletions lib/service/sfServiceContainer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id$
*/
class sfServiceContainer implements sfServiceContainerInterface, ArrayAccess, Iterator
class sfServiceContainer implements sfServiceContainerInterface
{
protected
$serviceIds = array(),
Expand Down Expand Up @@ -105,12 +105,17 @@ public function getParameters()
*/
public function getParameter($name)
{
if (!$this->hasParameter($name))
if ($this->hasParameter($name))
{
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
return $this->parameters[strtolower($name)];
}

return $this->parameters[strtolower($name)];
if (sfConfig::has($name))
{
return sfConfig::get($name);
}

throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
}

/**
Expand Down Expand Up @@ -206,146 +211,6 @@ public function getServiceIds()
return array_merge($ids, array_keys($this->services));
}

/**
* Returns true if the parameter name is defined (implements the ArrayAccess interface).
*
* @param string The parameter name
*
* @return Boolean true if the parameter name is defined, false otherwise
*/
public function offsetExists($name)
{
return $this->hasParameter($name);
}

/**
* Gets a service container parameter (implements the ArrayAccess interface).
*
* @param string The parameter name
*
* @return mixed The parameter value
*/
public function offsetGet($name)
{
return $this->getParameter($name);
}

/**
* Sets a parameter (implements the ArrayAccess interface).
*
* @param string The parameter name
* @param mixed The parameter value
*/
public function offsetSet($name, $value)
{
$this->setParameter($name, $value);
}

/**
* Removes a parameter (implements the ArrayAccess interface).
*
* @param string The parameter name
*/
public function offsetUnset($name)
{
unset($this->parameters[$name]);
}

/**
* Returns true if the container has a service with the given identifier.
*
* @param string The service identifier
*
* @return Boolean true if the container has a service with the given identifier, false otherwise
*/
public function __isset($id)
{
return $this->hasService($id);
}

/**
* Gets the service associated with the given identifier.
*
* @param string The service identifier
*
* @return mixed The service instance associated with the given identifier
*/
public function __get($id)
{
return $this->getService($id);
}

/**
* Sets a service.
*
* @param string The service identifier
* @param mixed A service instance
*/
public function __set($id, $service)
{
$this->setService($id, $service);
}

/**
* Removes a service by identifier.
*
* @param string The service identifier
*/
public function __unset($id)
{
throw new LogicException('You can\'t unset a service.');
}

/**
* Resets the service identifiers array to the beginning (implements the Iterator interface).
*/
public function rewind()
{
$this->serviceIds = $this->getServiceIds();

$this->count = count($this->serviceIds);
}

/**
* Gets the key associated with the current service (implements the Iterator interface).
*
* @return string The service identifier
*/
public function key()
{
return current($this->serviceIds);
}

/**
* Returns the current service (implements the Iterator interface).
*
* @return mixed The service
*/
public function current()
{
return $this->getService(current($this->serviceIds));
}

/**
* Moves to the next service (implements the Iterator interface).
*/
public function next()
{
next($this->serviceIds);

--$this->count;
}

/**
* Returns true if the current service is valid (implements the Iterator interface).
*
* @return boolean The validity of the current service; true if it is valid
*/
public function valid()
{
return $this->count > 0;
}

static public function camelize($id)
{
return preg_replace(array('/(^|_|-)+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $id);
Expand Down
16 changes: 2 additions & 14 deletions lib/service/sfServiceContainerBuilder.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,7 @@ public function resolveValue($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);
$value = $this->getParameter($match[1]);
}
else
{
Expand Down Expand Up @@ -340,11 +333,6 @@ public function resolveServices($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);
return $this->getParameter($match[2]);
}
}
4 changes: 2 additions & 2 deletions lib/service/sfServiceContainerDumperGraphviz.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected function findNodes()
$container->setServiceDefinition($id, new sfServiceDefinition('stdClass'));
}

foreach ($container as $id => $service)
foreach ($container->getServiceIds() as $id)
{
if (in_array($id, array_keys($container->getAliases())))
{
Expand All @@ -159,7 +159,7 @@ protected function findNodes()

if (!$container->hasServiceDefinition($id))
{
$nodes[$id] = array('class' => get_class($service), 'attributes' => $this->options['node.instance']);
$nodes[$id] = array('class' => get_class($container->getService($id)), 'attributes' => $this->options['node.instance']);
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/unit/service/sfServiceContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
$t->ok($builder->hasService('foo'), '->hasService() returns true if a service definition exists');
$builder->setAlias('foobar', 'foo');
$t->ok($builder->hasService('foo'), '->hasService() returns true if a service definition exists');
$builder->bar = new stdClass();
$builder->setService('bar', new stdClass());
$t->ok($builder->hasService('bar'), '->hasService() returns true if a service exists');
$builder->setAlias('foobar2', 'foo');
$t->ok($builder->hasService('foobar2'), '->hasService() returns true if a service exists');
Expand All @@ -94,7 +94,7 @@
}
$builder->register('foo', 'stdClass');
$t->ok(is_object($builder->getService('foo')), '->getService() returns the service definition associated with the id');
$builder->bar = $bar = new stdClass();
$builder->setService('bar', $bar = new stdClass());
$t->is($builder->getService('bar'), $bar, '->getService() returns the service associated with the id');
$builder->register('bar', 'stdClass');
$t->is($builder->getService('bar'), $bar, '->getService() returns the service associated with the id even if a definition has been defined');
Expand Down Expand Up @@ -208,21 +208,21 @@
try
{
$builder->resolveValue('%foobar%');
$t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
$t->fail('->resolveValue() throws a InvalidArgumentException if a placeholder references a non-existant parameter');
}
catch (RuntimeException $e)
catch (InvalidArgumentException $e)
{
$t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
$t->pass('->resolveValue() throws a InvalidArgumentException if a placeholder references a non-existant parameter');
}

try
{
$builder->resolveValue('foo %foobar% bar');
$t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
$t->fail('->resolveValue() throws a InvalidArgumentException if a placeholder references a non-existant parameter');
}
catch (RuntimeException $e)
catch (InvalidArgumentException $e)
{
$t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
$t->pass('->resolveValue() throws a InvalidArgumentException if a placeholder references a non-existant parameter');
}

// ->resolveServices()
Expand Down
66 changes: 2 additions & 64 deletions test/unit/service/sfServiceContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

require_once(dirname(__FILE__).'/../../bootstrap/unit.php');

$t = new lime_test(41);
$t = new lime_test(27);

// __construct()
$t->diag('__construct()');
Expand Down Expand Up @@ -41,21 +41,14 @@
$sc = new sfServiceContainer(array('foo' => 'bar'));
$sc->setParameter('bar', 'foo');
$t->is($sc->getParameter('bar'), 'foo', '->setParameter() sets the value of a new parameter');
$t->is($sc['bar'], 'foo', '->offsetGet() gets the value of a parameter');

$sc['bar1'] = 'foo1';
$t->is($sc['bar1'], 'foo1', '->offsetset() sets the value of a parameter');

unset($sc['bar1']);
$t->ok(!isset($sc['bar1']), '->offsetUnset() removes a parameter');
$t->is($sc->getParameter('bar'), 'foo', '->getParameter() gets the value of a parameter');

$sc->setParameter('foo', 'baz');
$t->is($sc->getParameter('foo'), 'baz', '->setParameter() overrides previously set parameter');

$sc->setParameter('Foo', 'baz1');
$t->is($sc->getParameter('foo'), 'baz1', '->setParameter() converts the key to lowercase');
$t->is($sc->getParameter('FOO'), 'baz1', '->getParameter() converts the key to lowercase');
$t->is($sc['FOO'], 'baz1', '->offsetGet() converts the key to lowercase');

try
{
Expand All @@ -67,25 +60,12 @@
$t->pass('->getParameter() thrown an InvalidArgumentException if the key does not exist');
}

try
{
$sc['baba'];
$t->fail('->offsetGet() thrown an InvalidArgumentException if the key does not exist');
}
catch (InvalidArgumentException $e)
{
$t->pass('->offsetGet() thrown an InvalidArgumentException if the key does not exist');
}

// ->hasParameter()
$t->diag('->hasParameter()');
$sc = new sfServiceContainer(array('foo' => 'bar'));
$t->ok($sc->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined');
$t->ok($sc->hasParameter('Foo'), '->hasParameter() converts the key to lowercase');
$t->ok(isset($sc['Foo']), '->offsetExists() converts the key to lowercase');
$t->ok(!$sc->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined');
$t->ok(isset($sc['foo']), '->offsetExists() returns true if a parameter is defined');
$t->ok(!isset($sc['bar']), '->offsetExists() returns false if a parameter is not defined');

// ->addParameters()
$t->diag('->addParameters()');
Expand All @@ -102,13 +82,8 @@
$t->is(spl_object_hash($sc->getService('foo')), spl_object_hash($obj), '->setService() registers a service under a key name');

$sc->foo1 = $obj1 = new stdClass();
$t->is(spl_object_hash($sc->foo1), spl_object_hash($obj1), '->__set() sets a service');

$t->is(spl_object_hash($sc->foo), spl_object_hash($obj), '->__get() gets a service by name');
$t->ok($sc->hasService('foo'), '->hasService() returns true if the service is defined');
$t->ok(isset($sc->foo), '->__isset() returns true if the service is defined');
$t->ok(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined');
$t->ok(!isset($sc->bar), '->__isset() returns false if the service is not defined');

// ->getServiceIds()
$t->diag('->getServiceIds()');
Expand Down Expand Up @@ -163,42 +138,5 @@ protected function getFoo_BazService()
$t->pass('->getService() thrown an InvalidArgumentException if the service does not exist');
}

try
{
$sc->baba;
$t->fail('->__get() thrown an InvalidArgumentException if the service does not exist');
}
catch (InvalidArgumentException $e)
{
$t->pass('->__get() thrown an InvalidArgumentException if the service does not exist');
}

try
{
unset($sc->baba);
$t->fail('->__unset() thrown an LogicException if you try to remove a service');
}
catch (LogicException $e)
{
$t->pass('->__unset() thrown an LogicException if you try to remove a service');
}

$t->is(spl_object_hash($sc->getService('foo_bar')), spl_object_hash($sc->__foo_bar), '->getService() camelizes the service id when looking for a method');
$t->is(spl_object_hash($sc->getService('foo.baz')), spl_object_hash($sc->__foo_baz), '->getService() camelizes the service id when looking for a method');

// Iterator
$t->diag('implements Iterator');
$sc = new ProjectServiceContainer();
$sc->setService('foo', $foo = new stdClass());
$services = array();
foreach ($sc as $id => $service)
{
$services[$id] = spl_object_hash($service);
}
$t->is($services, array(
'service_container' => spl_object_hash($sc),
'bar' => spl_object_hash($sc->__bar),
'foo_bar' => spl_object_hash($sc->__foo_bar),
'foo.baz' => spl_object_hash($sc->__foo_baz),
'foo' => spl_object_hash($foo)),
'sfServiceContainer implements the Iterator interface');
Loading