addMethodCall() public method

Adds a method to call to be injected on any service implementing the interface.
public addMethodCall ( string $method, array $arguments = [] ) : InterfaceInjector
$method string The method name to call
$arguments array An array of arguments to pass to the method call
return InterfaceInjector The current instance
Example #1
0
 public function testInterfaceInjectors()
 {
     $interfaceInjector = new InterfaceInjector('FooClass');
     $interfaceInjector->addMethodCall('setBar', array('someValue'));
     $container = (include self::$fixturesPath . '/containers/interfaces1.php');
     $container->addInterfaceInjector($interfaceInjector);
     $dumper = new XmlDumper($container);
     $classBody = $dumper->dump();
     //TODO: find a better way to test dumper
     //var_dump($classBody);
     $this->assertEquals("<?xml version=\"1.0\" ?>\n\n<container xmlns=\"http://www.symfony-project.org/schema/dic/services\"\n    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n    xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\">\n  <parameters>\n    <parameter key=\"cla\">Fo</parameter>\n    <parameter key=\"ss\">Class</parameter>\n  </parameters>\n  <interfaces>\n    <interface class=\"FooClass\">\n      <call method=\"setBar\">\n        <argument>someValue</argument>\n      </call>\n    </interface>\n  </interfaces>\n  <services>\n    <service id=\"foo\" class=\"%cla%o%ss%\">\n    </service>\n  </services>\n</container>\n", $classBody);
 }
 public function testInterfaceInjectors()
 {
     $interfaceInjector = new InterfaceInjector('FooClass');
     $interfaceInjector->addMethodCall('setBar', array('someValue'));
     $container = (include self::$fixturesPath . '/containers/interfaces1.php');
     $container->addInterfaceInjector($interfaceInjector);
     $dumper = new YamlDumper($container);
     $classBody = $dumper->dump();
     //TODO: find a better way to test dumper
     //var_dump($classBody);
     $this->assertEquals("parameters:\n  cla: Fo\n  ss: Class\n\ninterfaces:\n    FooClass:\n        calls:\n          - [setBar, [someValue]]\n          \n\nservices:\n  foo:\n    class: %cla%o%ss%\n", $classBody);
 }
Example #3
0
 public function testInterfaceInjectors()
 {
     $interfaceInjector = new InterfaceInjector('FooClass');
     $interfaceInjector->addMethodCall('setBar', array('someValue'));
     $container = (include self::$fixturesPath . '/containers/interfaces1.php');
     $container->addInterfaceInjector($interfaceInjector);
     $dumper = new XmlDumper($container);
     $classBody = $dumper->dump();
     //TODO: find a better way to test dumper
     //var_dump($classBody);
     $this->assertEquals("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<container xmlns=\"http://www.symfony-project.org/schema/dic/services\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\">\n  <parameters>\n    <parameter key=\"cla\">Fo</parameter>\n    <parameter key=\"ss\">Class</parameter>\n  </parameters>\n  <services>\n    <service id=\"foo\" class=\"%cla%o%ss%\"/>\n  </services>\n  <interfaces>\n    <interface class=\"FooClass\">\n      <call method=\"setBar\">\n        <argument>someValue</argument>\n      </call>\n    </interface>\n  </interfaces>\n</container>\n", $classBody);
     $dom = new \DOMDocument();
     $dom->loadXML($classBody);
     $this->assertTrue($dom->schemaValidate(__DIR__ . '/../../../../../../src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd'));
 }
Example #4
0
 /**
  * Parses an interface injector.
  *
  * @param string $class
  * @param array $interface
  * @param string $file
  * @return void
  */
 private function parseInterfaceInjector($class, $interface, $file)
 {
     $injector = new InterfaceInjector($class);
     if (isset($interface['calls'])) {
         foreach ($interface['calls'] as $call) {
             $injector->addMethodCall($call[0], $this->resolveServices($call[1]));
         }
     }
     $this->container->addInterfaceInjector($injector);
 }
Example #5
0
 /**
  * Parses an individual interface injector
  *
  * @param string $class
  * @param SimpleXMLElement $interface
  * @param string $file
  */
 protected function parseInterfaceInjector($class, $interface, $file)
 {
     $injector = new InterfaceInjector($class);
     foreach ($interface->call as $call) {
         $injector->addMethodCall((string) $call['method'], $call->getArgumentsAsPhp('argument'));
     }
     $this->container->addInterfaceInjector($injector);
 }
Example #6
0
 public function testFrozenContainerInterfaceInjectors()
 {
     $interfaceInjector = new InterfaceInjector('FooClass');
     $interfaceInjector->addMethodCall('setBar', array('someValue'));
     $container = (include self::$fixturesPath . '/containers/interfaces1.php');
     $container->addInterfaceInjector($interfaceInjector);
     $container->compile();
     $dumper = new PhpDumper($container);
     file_put_contents(self::$fixturesPath . '/php/services_interfaces-1-1.php', $dumper->dump());
     $this->assertStringEqualsFile(self::$fixturesPath . '/php/services_interfaces-1-1.php', $dumper->dump(), '->dump() dumps interface injectors');
 }
 public function getInjectorsAndDefinitions()
 {
     $injector = new InterfaceInjector('Symfony\\Tests\\Component\\DependencyInjection\\Service');
     $injector->addMethodCall('method');
     $injector->addMethodCall('method');
     $injector->addMethodCall('method');
     $injector->addMethodCall('method');
     $definition1 = $this->getMockDefinition('stdClass', 0);
     $definition2 = $this->getMockDefinition('Symfony\\Tests\\Component\\DependencyInjection\\Service', 4);
     return array(array($injector, $definition1), array($injector, $definition2));
 }