浏览代码

removed ArrayAccess interface for Container and Controller

Fabien Potencier 14 年之前
父节点
当前提交
f6cc63c99c

+ 6 - 27
src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

@@ -22,7 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerAware;
  *
  * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  */
-class Controller extends ContainerAware implements \ArrayAccess
+class Controller extends ContainerAware
 {
     /**
      * Creates a Response instance.
@@ -113,47 +113,26 @@ class Controller extends ContainerAware implements \ArrayAccess
     }
 
     /**
-     * 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)
     {
         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));
-    }
 }

+ 1 - 46
src/Symfony/Component/DependencyInjection/Container.php

@@ -52,7 +52,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
  *
  * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  */
-class Container implements ContainerInterface, \ArrayAccess
+class Container implements ContainerInterface
 {
     protected $parameterBag;
     protected $services;
@@ -216,51 +216,6 @@ class Container implements ContainerInterface, \ArrayAccess
         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.
      *

+ 0 - 29
tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php

@@ -117,32 +117,18 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
         }
     }
 
-    /**
-     * @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()
     {
@@ -171,19 +157,10 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
             $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()
     {
@@ -194,12 +171,6 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
         $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');
     }
 }