Skip to content

Commit

Permalink
removed ArrayAccess interface for Container and Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Nov 15, 2010
1 parent 53dd4e3 commit f6cc63c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 102 deletions.
33 changes: 6 additions & 27 deletions src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
Expand Up @@ -22,7 +22,7 @@
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Controller extends ContainerAware implements \ArrayAccess
class Controller extends ContainerAware
{
/**
* Creates a Response instance.
Expand Down Expand Up @@ -113,47 +113,26 @@ public function render($view, array $parameters = array(), Response $response =
}

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

/**
* Gets a service by id (implements the ArrayAccess interface).
* Gets a service by id.
*
* @param string $id The service id
*
* @return mixed The parameter value
* @return object The service
*/
public function offsetGet($id)
public function get($id)

This comment has been minimized.

Copy link
@umpirsky

umpirsky Apr 12, 2011

Contributor

I think it would be nice to expose some method to list available services or get container.

Use case:

I wanted to get kernel environment, but I don't know how to get kernel and I can't list services. If you expose getServiceIds or container, I can see what services are available and what are their Ids. I lost some time to figure that I need to $this->get('kernel')->getEnvironment();

This comment has been minimized.

Copy link
@Seldaek

Seldaek Apr 12, 2011

Member

For this particular case, you can also do $this->container->getParameter('kernel.environment')

This comment has been minimized.

Copy link
@umpirsky

umpirsky Apr 12, 2011

Contributor

Ah yes, I can access container directly, silly me: $this->container->getServiceIds(). Thanks!

{
return $this->container->get($id);
}

/**
* Sets a service (implements the ArrayAccess interface).
*
* @param string $id The service id
* @param object $value The service
*/
public function offsetSet($id, $value)
{
throw new \LogicException(sprintf('You can\'t set a service from a controller (%s).', $id));
}

/**
* Removes a service (implements the ArrayAccess interface).
*
* @param string $id The service id
*/
public function offsetUnset($id)
{
throw new \LogicException(sprintf('You can\'t unset a service from a controller (%s).', $id));
}
}
47 changes: 1 addition & 46 deletions src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -52,7 +52,7 @@
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Container implements ContainerInterface, \ArrayAccess
class Container implements ContainerInterface
{
protected $parameterBag;
protected $services;
Expand Down Expand Up @@ -216,51 +216,6 @@ public function getServiceIds()
return array_merge($ids, array_keys($this->services));
}

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

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

/**
* Sets a service (implements the ArrayAccess interface).
*
* @param string $id The service id
* @param object $value The service
*/
public function offsetSet($id, $value)
{
$this->set($id, $value);
}

/**
* Removes a service (implements the ArrayAccess interface).
*
* @param string $id The service id
*/
public function offsetUnset($id)
{
throw new \LogicException(sprintf('You can\'t unset a service (%s).', $id));
}

/**
* Catches unknown methods.
*
Expand Down
Expand Up @@ -117,32 +117,18 @@ public function testGetCall()
}
}

/**
* @covers Symfony\Component\DependencyInjection\Container::offsetUnset
* @expectedException LogicException
*/
public function testOffetUnset()
{
$sc = new Container();
unset($sc['foo']);
}

/**
* @covers Symfony\Component\DependencyInjection\Container::set
* @covers Symfony\Component\DependencyInjection\Container::offsetSet
*/
public function testSet()
{
$sc = new Container();
$sc->set('foo', $foo = new \stdClass());
$this->assertEquals($foo, $sc->get('foo'), '->set() sets a service');
$sc['bar'] = $foo = new \stdClass();
$this->assertEquals($foo, $sc->get('bar'), '->offsetSet() sets a service');
}

/**
* @covers Symfony\Component\DependencyInjection\Container::get
* @covers Symfony\Component\DependencyInjection\Container::offsetGet
*/
public function testGet()
{
Expand Down Expand Up @@ -171,19 +157,10 @@ public function testGet()
$this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
}
$this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));

try {
$sc[''];
$this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service is empty');
$this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
}
}

/**
* @covers Symfony\Component\DependencyInjection\Container::has
* @covers Symfony\Component\DependencyInjection\Container::offsetExists
*/
public function testHas()
{
Expand All @@ -194,12 +171,6 @@ public function testHas()
$this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
$this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');

$this->assertFalse(isset($sc['foo1']), '->offsetExists() returns false if the service does not exist');
$this->assertTrue(isset($sc['foo']), '->offsetExists() returns true if the service exists');
$this->assertTrue(isset($sc['bar']), '->offsetExists() returns true if a get*Method() is defined');
$this->assertTrue(isset($sc['foo_bar']), '->offsetExists() returns true if a get*Method() is defined');
$this->assertTrue(isset($sc['foo.baz']), '->offsetExists() returns true if a get*Method() is defined');
}
}

Expand Down

0 comments on commit f6cc63c

Please sign in to comment.