Changeset 31585
- Timestamp:
- 12/01/10 12:15:09 (2 years ago)
- Files:
-
- branches/2.0/src/Symfony/Bundle/TwigBundle/Resources/views/form.twig (modified) (2 diffs)
- branches/2.0/src/Symfony/Component/DependencyInjection/ContainerBuilder.php (modified) (7 diffs)
- branches/2.0/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php (modified) (3 diffs)
- branches/2.0/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php (modified) (3 diffs)
- branches/2.0/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php (modified) (1 diff)
- branches/2.0/src/Symfony/Component/DependencyInjection/Exception (added)
- branches/2.0/src/Symfony/Component/DependencyInjection/Exception/Exception.php (added)
- branches/2.0/src/Symfony/Component/DependencyInjection/Exception/InvalidArgumentException.php (added)
- branches/2.0/src/Symfony/Component/DependencyInjection/InterfaceInjector.php (added)
- branches/2.0/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php (modified) (4 diffs)
- branches/2.0/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php (modified) (5 diffs)
- branches/2.0/src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd (modified) (2 diffs)
- branches/2.0/src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php (modified) (3 diffs)
- branches/2.0/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php (modified) (2 diffs)
- branches/2.0/src/Symfony/Component/Routing/Route.php (modified) (3 diffs)
- branches/2.0/src/Symfony/Component/Yaml/Parser.php (modified) (2 diffs)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/ContainerBuilderTest.php (modified) (1 diff)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Dumper/PhpDumperTest.php (modified) (2 diffs)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Dumper/XmlDumperTest.php (modified) (2 diffs)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Dumper/YamlDumperTest.php (modified) (2 diffs)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/interfaces1.php (added)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/containers/interfaces2.php (added)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services1-1.php (modified) (1 diff)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services1.php (modified) (1 diff)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services8.php (modified) (1 diff)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php (modified) (1 diff)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services_interfaces-1-1.php (copied) (copied from branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services8.php) (4 diffs, 1 prop)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services_interfaces-1.php (copied) (copied from branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services8.php) (3 diffs, 1 prop)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services_interfaces-2.php (added)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/xml/interfaces1.xml (added)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/yaml/interfaces1.yml (added)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/InterfaceInjectorTest.php (added)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Loader/XmlFileLoaderTest.php (modified) (2 diffs)
- branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Loader/YamlFileLoaderTest.php (modified) (2 diffs)
- branches/2.0/tests/Symfony/Tests/Component/Yaml/Fixtures/sfTests.yml (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/2.0/src/Symfony/Bundle/TwigBundle/Resources/views/form.twig
r31482 r31585 17 17 <ul> 18 18 {% for error in field.errors %} 19 <li>{% trans error.messageTemplate with error.messageParameters from validators%}</li>19 <li>{% trans error.messageTemplate with error.messageParameters from 'validators' %}</li> 20 20 {% endfor %} 21 21 </ul> … … 142 142 {{ group.original_name|render }} 143 143 {% endblock file_field %} 144 branches/2.0/src/Symfony/Component/DependencyInjection/ContainerBuilder.php
r31229 r31585 6 6 use Symfony\Component\DependencyInjection\Resource\ResourceInterface; 7 7 use Symfony\Component\DependencyInjection\Resource\FileResource; 8 use Symfony\Component\DependencyInjection\InterfaceInjector; 8 9 9 10 /* … … 30 31 protected $resources = array(); 31 32 protected $extensionConfigs = array(); 33 protected $injectors = array(); 32 34 33 35 /** … … 131 133 * @param string $id The service identifier 132 134 * @param object $service The service instance 135 * 136 * @throws BadMethodCallException 133 137 */ 134 138 public function set($id, $service) 135 139 { 140 if ($this->isFrozen()) { 141 throw new \BadMethodCallException('Setting service on a frozen container is not allowed'); 142 } 143 136 144 unset($this->definitions[$id]); 137 145 unset($this->aliases[$id]); … … 288 296 $this->merge($container); 289 297 } 298 290 299 $this->extensionConfigs = array(); 291 292 300 $this->addDefinitions($definitions); 293 301 $this->addAliases($aliases); 294 302 $this->parameterBag->add($parameters); 295 303 304 foreach ($this->definitions as $definition) { 305 foreach ($this->injectors as $injector) { 306 if (null !== $definition->getFactoryService()) { 307 continue; 308 } 309 $defClass = $this->parameterBag->resolveValue($definition->getClass()); 310 $definition->setClass($defClass); 311 if ($injector->supports($defClass)) { 312 $injector->processDefinition($definition); 313 } 314 } 315 } 316 296 317 parent::freeze(); 297 318 } … … 393 414 } 394 415 416 public function addInterfaceInjectors(array $injectors) 417 { 418 foreach ($injectors as $injector) { 419 $this->addInterfaceInjector($injector); 420 } 421 } 422 423 public function addInterfaceInjector(InterfaceInjector $injector) 424 { 425 $class = $injector->getClass(); 426 if (isset($this->injectors[$class])) { 427 return $this->injectors[$class]->merge($injector); 428 } 429 430 $this->injectors[$class] = $injector; 431 } 432 433 public function getInterfaceInjectors($service = null) 434 { 435 if (null === $service) { 436 return $this->injectors; 437 } 438 439 return array_filter($this->injectors, function(InterfaceInjector $injector) use ($service) { 440 return $injector->supports($service); 441 }); 442 } 443 395 444 /** 396 445 * Registers a service definition. … … 447 496 * @param string $id The service identifier 448 497 * @param Definition $definition A Definition instance 498 * 499 * @throws BadMethodCallException 449 500 */ 450 501 public function setDefinition($id, Definition $definition) 451 502 { 503 if ($this->isFrozen()) { 504 throw new \BadMethodCallException('Adding definition to a frozen container is not allowed'); 505 } 506 452 507 unset($this->aliases[$id]); 453 508 … … 535 590 536 591 $service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments); 592 } 593 594 foreach ($this->getInterfaceInjectors($service) as $injector) { 595 $injector->processDefinition($definition, $service); 537 596 } 538 597 branches/2.0/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
r31539 r31585 50 50 $this->addTags(). 51 51 $this->addDefaultParametersMethod(). 52 $this->addInterfaceInjectors(). 52 53 $this->endClass() 53 54 ; 55 } 56 57 protected function addInterfaceInjectors() 58 { 59 if ($this->container->isFrozen() || 0 === count($this->container->getInterfaceInjectors())) { 60 return; 61 } 62 63 $code = <<<EOF 64 65 /** 66 * Applies all known interface injection calls 67 * 68 * @param Object \$instance 69 */ 70 protected function applyIntrefaceInjectors(\$instance) 71 { 72 73 EOF; 74 foreach ($this->container->getInterfaceInjectors() as $injector) { 75 $code .= sprintf(" if (\$instance instanceof \\%s) {\n", $injector->getClass()); 76 foreach ($injector->getMethodCalls() as $call) { 77 foreach ($call[1] as $value) { 78 $arguments[] = $this->dumpValue($value); 79 } 80 $code .= $this->wrapServiceConditionals($call[1], sprintf(" \$instance->%s(%s);\n", $call[0], implode(', ', $arguments))); 81 } 82 $code .= sprintf(" }\n"); 83 } 84 $code .= <<<EOF 85 } 86 87 EOF; 88 return $code; 54 89 } 55 90 … … 124 159 125 160 $calls .= $this->wrapServiceConditionals($call[1], sprintf(" \$instance->%s(%s);\n", $call[0], implode(', ', $arguments))); 161 } 162 163 if (!$this->container->isFrozen() && count($this->container->getInterfaceInjectors()) > 0) { 164 $calls = sprintf("\n \$this->applyInterfaceInjection(\$instance);\n"); 126 165 } 127 166 … … 241 280 } 242 281 } 243 $tags = var_export($tags, true);282 $tags = $this->exportParameters($tags); 244 283 245 284 return <<<EOF branches/2.0/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
r31229 r31585 6 6 use Symfony\Component\DependencyInjection\Parameter; 7 7 use Symfony\Component\DependencyInjection\Reference; 8 use Symfony\Component\DependencyInjection\InterfaceInjector; 8 9 9 10 /* … … 32 33 public function dump(array $options = array()) 33 34 { 34 return $this->startXml().$this->addParameters().$this->add Services().$this->endXml();35 return $this->startXml().$this->addParameters().$this->addInterfaceInjectors().$this->addServices().$this->endXml(); 35 36 } 36 37 … … 48 49 49 50 return sprintf(" <parameters>\n%s </parameters>\n", $this->convertParameters($parameters, 'parameter', 4)); 51 } 52 53 protected function addInterfaceInjector(InterfaceInjector $injector) 54 { 55 $code = \sprintf(" <interface class=\"%s\">\n", $injector->getClass()); 56 57 foreach ($injector->getMethodCalls() as $call) { 58 if (count($call[1])) { 59 $code .= sprintf(" <call method=\"%s\">\n%s </call>\n", $call[0], $this->convertParameters($call[1], 'argument', 8)); 60 } else { 61 $code .= sprintf(" <call method=\"%s\" />\n", $call[0]); 62 } 63 } 64 65 $code .= " </interface>\n"; 66 67 return $code; 68 } 69 70 protected function addInterfaceInjectors() 71 { 72 if (!$this->container->getInterfaceInjectors()) { 73 return ''; 74 } 75 76 $code = ''; 77 foreach ($this->container->getInterfaceInjectors() as $injector) { 78 $code .= $this->addInterfaceInjector($injector); 79 } 80 81 return sprintf(" <interfaces>\n%s </interfaces>\n", $code); 50 82 } 51 83 branches/2.0/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
r31403 r31585 33 33 public function dump(array $options = array()) 34 34 { 35 return $this->addParameters()."\n".$this->addServices(); 35 return $this->addParameters().$this->addInterfaceInjectors()."\n".$this->addServices(); 36 } 37 38 protected function addInterfaceInjectors() 39 { 40 if (!$this->container->getInterfaceInjectors()) { 41 return ''; 42 } 43 44 $code = "\ninterfaces:\n"; 45 foreach ($this->container->getInterfaceInjectors() as $injector) { 46 $code .= sprintf(" %s:\n", $injector->getClass()); 47 if ($injector->getMethodCalls()) { 48 $code .= sprintf(" calls:\n %s\n", str_replace("\n", "\n ", Yaml::dump($this->dumpValue($injector->getMethodCalls()), 1))); 49 } 50 } 51 52 return $code; 36 53 } 37 54 branches/2.0/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
r31403 r31585 3 3 namespace Symfony\Component\DependencyInjection\Loader; 4 4 5 use Symfony\Component\DependencyInjection\InterfaceInjector; 5 6 use Symfony\Component\DependencyInjection\Definition; 6 7 use Symfony\Component\DependencyInjection\Reference; … … 50 51 $this->parseParameters($xml, $file); 51 52 53 // interface injectors 54 $this->parseInterfaceInjectors($xml, $file); 55 52 56 // services 53 57 $this->parseDefinitions($xml, $file); … … 85 89 $this->import((string) $import['resource'], (Boolean) $import->getAttributeAsPhp('ignore-errors')); 86 90 } 91 } 92 93 protected function parseInterfaceInjectors($xml, $file) 94 { 95 if (!$xml->interfaces) { 96 return; 97 } 98 99 foreach ($xml->interfaces->interface as $interface) { 100 $this->parseInterfaceInjector((string) $interface['class'], $interface, $file); 101 } 102 } 103 104 protected function parseInterfaceInjector($class, $interface, $file) 105 { 106 $injector = new InterfaceInjector($class); 107 foreach ($interface->call as $call) { 108 $injector->addMethodCall((string) $call['method'], $call->getArgumentsAsPhp('argument')); 109 } 110 $this->container->addInterfaceInjector($injector); 87 111 } 88 112 … … 287 311 { 288 312 foreach ($dom->documentElement->childNodes as $node) { 289 if (!$node instanceof \DOMElement || in_array($node->tagName, array('imports', 'parameters', 'services' ))) {313 if (!$node instanceof \DOMElement || in_array($node->tagName, array('imports', 'parameters', 'services', 'interfaces'))) { 290 314 continue; 291 315 } branches/2.0/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
r31403 r31585 4 4 5 5 use Symfony\Component\DependencyInjection\ContainerInterface; 6 use Symfony\Component\DependencyInjection\InterfaceInjector; 6 7 use Symfony\Component\DependencyInjection\Definition; 7 8 use Symfony\Component\DependencyInjection\Reference; … … 58 59 } 59 60 61 // interface injectors 62 $this->parseInterfaceInjectors($content, $file); 63 60 64 // services 61 65 $this->parseDefinitions($content, $file); … … 86 90 } 87 91 92 protected function parseInterfaceInjectors($content, $file) 93 { 94 if (!isset($content['interfaces'])) { 95 return; 96 } 97 98 foreach ($content['interfaces'] as $class => $interface) { 99 $this->parseInterfaceInjector($class, $interface, $file); 100 } 101 } 102 103 protected function parseInterfaceInjector($class, $interface, $file) 104 { 105 $injector = new InterfaceInjector($class); 106 if (isset($interface['calls'])) { 107 foreach ($interface['calls'] as $call) { 108 $injector->addMethodCall($call[0], $this->resolveServices($call[1])); 109 } 110 } 111 $this->container->addInterfaceInjector($injector); 112 } 113 88 114 protected function parseDefinitions($content, $file) 89 115 { … … 176 202 177 203 foreach (array_keys($content) as $key) { 178 if (in_array($key, array('imports', 'parameters', 'services' ))) {204 if (in_array($key, array('imports', 'parameters', 'services', 'interfaces'))) { 179 205 continue; 180 206 } … … 212 238 { 213 239 foreach ($content as $key => $values) { 214 if (in_array($key, array('imports', 'parameters', 'services' ))) {240 if (in_array($key, array('imports', 'parameters', 'services', 'interfaces'))) { 215 241 continue; 216 242 } branches/2.0/src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd
r30747 r31585 29 29 <xsd:element name="parameters" type="parameters" minOccurs="0" maxOccurs="1" /> 30 30 <xsd:element name="services" type="services" minOccurs="0" maxOccurs="1" /> 31 <xsd:element name="interfaces" type="interfaces" minOccurs="0" maxOccurs="1" /> 31 32 <xsd:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded" /> 33 </xsd:sequence> 34 </xsd:complexType> 35 36 <xsd:complexType name="interfaces"> 37 <xsd:annotation> 38 <xsd:documentation><![CDATA[ 39 Enclosing element for the definition of all interface injectors 40 ]]></xsd:documentation> 41 </xsd:annotation> 42 <xsd:sequence> 43 <xsd:element name="interface" type="interface" minOccurs="0" maxOccurs="unbounded" /> 32 44 </xsd:sequence> 33 45 </xsd:complexType> … … 71 83 <xsd:attribute name="method" type="xsd:string" /> 72 84 <xsd:attribute name="function" type="xsd:string" /> 85 </xsd:complexType> 86 87 <xsd:complexType name="interface"> 88 <xsd:sequence maxOccurs="unbounded"> 89 <xsd:element name="call" type="call" minOccurs="1" maxOccurs="unbounded" /> 90 </xsd:sequence> 91 <xsd:attribute name="class" type="xsd:string" use="required" /> 73 92 </xsd:complexType> 74 93 branches/2.0/src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php
r31539 r31585 51 51 { 52 52 $methods = array(); 53 53 $routes = array(); 54 54 foreach ($this->routes->all() as $name => $route) { 55 55 $compiledRoute = $route->compile(); … … 68 68 EOF 69 69 ; 70 71 $routes[] = " '$name' => true,"; 70 72 } 71 73 72 74 $methods = implode("\n", $methods); 75 $routes = implode("\n", $routes); 73 76 74 77 return <<<EOF … … 76 79 public function generate(\$name, array \$parameters, \$absolute = false) 77 80 { 78 if (!method_exists(\$this, \$method = 'get'.\$name.'RouteInfo')) { 81 static \$routes = array( 82 $routes 83 ); 84 85 if (!isset(\$routes[\$name])) { 79 86 throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', \$name)); 80 87 } 81 88 82 list(\$variables, \$defaults, \$requirements, \$tokens) = \$this-> \$method();89 list(\$variables, \$defaults, \$requirements, \$tokens) = \$this->{'get'.\$name.'RouteInfo'}(); 83 90 84 91 return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute); branches/2.0/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
r31539 r31585 117 117 } 118 118 119 $defaults = array_merge($globals['defaults'], $annot->getDefaults() , $this->getRouteDefaults($class, $method, $annot));119 $defaults = array_merge($globals['defaults'], $annot->getDefaults()); 120 120 $requirements = array_merge($globals['requirements'], $annot->getRequirements()); 121 121 $options = array_merge($globals['options'], $annot->getOptions()); 122 122 123 123 $route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options); 124 125 $this->configureRoute($route, $class, $method); 126 124 127 $collection->add($annot->getName(), $route); 125 128 } … … 164 167 } 165 168 166 abstract protected function getRouteDefaults(\ReflectionClass $class, \ReflectionMethod $method, RouteAnnotation $annot);169 abstract protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method); 167 170 } branches/2.0/src/Symfony/Component/Routing/Route.php
r31561 r31585 194 194 $this->requirements = array(); 195 195 foreach ($requirements as $key => $regex) { 196 if (is_array($regex)) { 197 throw new \InvalidArgumentException(sprintf('Routing requirements must be a string, array given for "%s"', $key)); 198 } 199 200 if ('^' == $regex[0]) { 201 $regex = substr($regex, 1); 202 } 203 204 if ('$' == substr($regex, -1)) { 205 $regex = substr($regex, 0, -1); 206 } 207 208 $this->requirements[$key] = $regex; 196 $this->requirements[$key] = $this->sanitizeRequirement($key, $regex); 209 197 } 210 198 … … 220 208 { 221 209 return isset($this->requirements[$key]) ? $this->requirements[$key] : null; 210 } 211 212 /** 213 * Sets a requirement for the given key. 214 * 215 * @param string The key 216 * @param string The regex 217 */ 218 public function setRequirement($key, $regex) 219 { 220 return $this->requirements[$key] = $this->sanitizeRequirement($key, $regex); 222 221 } 223 222 … … 241 240 return $this->compiled = static::$compilers[$class]->compile($this); 242 241 } 242 243 protected function sanitizeRequirement($key, $regex) 244 { 245 if (is_array($regex)) { 246 throw new \InvalidArgumentException(sprintf('Routing requirements must be a string, array given for "%s"', $key)); 247 } 248 249 if ('^' == $regex[0]) { 250 $regex = substr($regex, 1); 251 } 252 253 if ('$' == substr($regex, -1)) { 254 $regex = substr($regex, 0, -1); 255 } 256 257 return $regex; 258 } 243 259 } branches/2.0/src/Symfony/Component/Yaml/Parser.php
r31229 r31585 81 81 if (isset($values['leadspaces']) 82 82 && ' ' == $values['leadspaces'] 83 && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{ ].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches)83 && preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches) 84 84 ) { 85 85 // this is a compact notation element, add to next block and parse … … 98 98 } 99 99 } 100 } else if (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'" ].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {100 } else if (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) { 101 101 $key = Inline::parseScalar($values['key']); 102 102 branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/ContainerBuilderTest.php
r31561 r31585 407 407 $container->getExtension('no_registered'); 408 408 } 409 410 /** 411 * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addInterfaceInjector 412 * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getInterfaceInjectors 413 * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinition 414 */ 415 public function testInterfaceInjection() 416 { 417 $definition = new Definition('Symfony\Tests\Component\DependencyInjection\FooClass'); 418 419 $injector = $this->getMockInterfaceInjector('Symfony\Tests\Component\DependencyInjection\FooClass', 1); 420 $injector2 = $this->getMockInterfaceInjector('Symfony\Tests\Component\DependencyInjection\FooClass', 0); 421 422 $container = new ContainerBuilder(); 423 $container->addInterfaceInjector($injector); 424 $container->addInterfaceInjector($injector2); 425 $this->assertEquals(1, count($container->getInterfaceInjectors('Symfony\Tests\Component\DependencyInjection\FooClass'))); 426 427 $container->setDefinition('test', $definition); 428 $test = $container->get('test'); 429 } 430 431 /** 432 * @expectedException BadMethodCallException 433 */ 434 public function testThrowsExceptionWhenSetServiceOnAFrozenContainer() 435 { 436 $container = new ContainerBuilder(); 437 $container->freeze(); 438 $container->set('a', new \stdClass()); 439 } 440 441 /** 442 * @expectedException BadMethodCallException 443 */ 444 public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer() 445 { 446 $container = new ContainerBuilder(); 447 $container->freeze(); 448 $container->setDefinition('a', new Definition()); 449 } 450 451 /** 452 * @param string $class 453 * @param int $methodCallsCount 454 * @return Symfony\Component\DependencyInjection\InterfaceInjector 455 */ 456 private function getMockInterfaceInjector($class, $methodCallsCount) 457 { 458 $injector = $this->getMock('Symfony\Component\DependencyInjection\InterfaceInjector', array('processDefinition'), array('Symfony\Tests\Component\DependencyInjection\FooClass'), '', true, false); 459 $injector->expects($this->exactly($methodCallsCount)) 460 ->method('processDefinition') 461 ; 462 return $injector; 463 } 409 464 } 465 466 467 class FooClass {} branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Dumper/PhpDumperTest.php
r31486 r31585 15 15 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; 16 16 use Symfony\Component\DependencyInjection\Reference; 17 use Symfony\Component\DependencyInjection\InterfaceInjector; 17 18 18 19 class PhpDumperTest extends \PHPUnit_Framework_TestCase … … 92 93 $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service'); 93 94 } 95 96 public function testInterfaceInjectors() 97 { 98 $interfaceInjector = new InterfaceInjector('FooClass'); 99 $interfaceInjector->addMethodCall('setBar', array('someValue')); 100 $container = include self::$fixturesPath.'/containers/interfaces1.php'; 101 $container->addInterfaceInjector($interfaceInjector); 102 103 $dumper = new PhpDumper($container); 104 105 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-1.php', $dumper->dump(), '->dump() dumps interface injectors'); 106 } 107 108 public function testInterfaceInjectorsAndServiceFactories() 109 { 110 $interfaceInjector = new InterfaceInjector('BarClass'); 111 $interfaceInjector->addMethodCall('setFoo', array('someValue')); 112 $container = include self::$fixturesPath.'/containers/interfaces2.php'; 113 $container->addInterfaceInjector($interfaceInjector); 114 115 $dumper = new PhpDumper($container); 116 117 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-2.php', $dumper->dump(), '->dump() dumps interface injectors'); 118 } 119 120 public function testFrozenContainerInterfaceInjectors() 121 { 122 $interfaceInjector = new InterfaceInjector('FooClass'); 123 $interfaceInjector->addMethodCall('setBar', array('someValue')); 124 $container = include self::$fixturesPath.'/containers/interfaces1.php'; 125 $container->addInterfaceInjector($interfaceInjector); 126 $container->freeze(); 127 128 $dumper = new PhpDumper($container); 129 130 file_put_contents(self::$fixturesPath.'/php/services_interfaces-1-1.php', $dumper->dump()); 131 132 $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_interfaces-1-1.php', $dumper->dump(), '->dump() dumps interface injectors'); 133 } 94 134 } branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Dumper/XmlDumperTest.php
r30721 r31585 13 13 use Symfony\Component\DependencyInjection\ContainerBuilder; 14 14 use Symfony\Component\DependencyInjection\Dumper\XmlDumper; 15 use Symfony\Component\DependencyInjection\InterfaceInjector; 15 16 16 17 class XmlDumperTest extends \PHPUnit_Framework_TestCase … … 63 64 } 64 65 } 66 67 public function testInterfaceInjectors() 68 { 69 $interfaceInjector = new InterfaceInjector('FooClass'); 70 $interfaceInjector->addMethodCall('setBar', array('someValue')); 71 $container = include self::$fixturesPath.'/containers/interfaces1.php'; 72 $container->addInterfaceInjector($interfaceInjector); 73 74 $dumper = new XmlDumper($container); 75 76 $classBody = $dumper->dump(); 77 //TODO: find a better way to test dumper 78 //var_dump($classBody); 79 80 $this->assertEquals("<?xml version=\"1.0\" ?> 81 82 <container xmlns=\"http://www.symfony-project.org/schema/dic/services\" 83 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 84 xsi:schemaLocation=\"http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd\"> 85 <parameters> 86 <parameter key=\"cla\">Fo</parameter> 87 <parameter key=\"ss\">Class</parameter> 88 </parameters> 89 <interfaces> 90 <interface class=\"FooClass\"> 91 <call method=\"setBar\"> 92 <argument>someValue</argument> 93 </call> 94 </interface> 95 </interfaces> 96 <services> 97 <service id=\"foo\" class=\"%cla%o%ss%\"> 98 </service> 99 </services> 100 </container> 101 ", $classBody); 102 } 65 103 } branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Dumper/YamlDumperTest.php
r30721 r31585 13 13 use Symfony\Component\DependencyInjection\ContainerBuilder; 14 14 use Symfony\Component\DependencyInjection\Dumper\YamlDumper; 15 use Symfony\Component\DependencyInjection\InterfaceInjector; 15 16 16 17 class YamlDumperTest extends \PHPUnit_Framework_TestCase … … 56 57 } 57 58 } 59 60 public function testInterfaceInjectors() 61 { 62 $interfaceInjector = new InterfaceInjector('FooClass'); 63 $interfaceInjector->addMethodCall('setBar', array('someValue')); 64 $container = include self::$fixturesPath.'/containers/interfaces1.php'; 65 $container->addInterfaceInjector($interfaceInjector); 66 67 $dumper = new YamlDumper($container); 68 69 $classBody = $dumper->dump(); 70 //TODO: find a better way to test dumper 71 //var_dump($classBody); 72 73 $this->assertEquals("parameters: 74 cla: Fo 75 ss: Class 76 77 interfaces: 78 FooClass: 79 calls: 80 - [setBar, [someValue]] 81 82 83 services: 84 foo: 85 class: %cla%o%ss% 86 ", $classBody); 87 } 58 88 } branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services1-1.php
r31486 r31585 33 33 public function findTaggedServiceIds($name) 34 34 { 35 static $tags = array ( 36 ); 35 static $tags = array( 36 37 ); 37 38 38 39 return isset($tags[$name]) ? $tags[$name] : array(); branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services1.php
r31486 r31585 33 33 public function findTaggedServiceIds($name) 34 34 { 35 static $tags = array ( 36 ); 35 static $tags = array( 36 37 ); 37 38 38 39 return isset($tags[$name]) ? $tags[$name] : array(); branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services8.php
r31486 r31585 33 33 public function findTaggedServiceIds($name) 34 34 { 35 static $tags = array ( 36 ); 35 static $tags = array( 36 37 ); 37 38 38 39 return isset($tags[$name]) ? $tags[$name] : array(); branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php
r31539 r31585 142 142 public function findTaggedServiceIds($name) 143 143 { 144 static $tags = array ( 145 'foo' => 146 array ( 147 'foo' => 148 array ( 149 0 => 150 array ( 151 'foo' => 'foo', 152 ), 153 1 => 154 array ( 155 'bar' => 'bar', 156 ), 157 ), 158 ), 159 ); 144 static $tags = array( 145 'foo' => array( 146 'foo' => array( 147 0 => array( 148 'foo' => 'foo', 149 ), 150 1 => array( 151 'bar' => 'bar', 152 ), 153 ), 154 ), 155 ); 160 156 161 157 return isset($tags[$name]) ? $tags[$name] : array(); branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services_interfaces-1-1.php
- Property svn:executable set to *
r31486 r31585 6 6 use Symfony\Component\DependencyInjection\Reference; 7 7 use Symfony\Component\DependencyInjection\Parameter; 8 use Symfony\Component\DependencyInjection\ParameterBag\ ParameterBag;8 use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag; 9 9 10 10 /** … … 21 21 public function __construct() 22 22 { 23 parent::__construct(new ParameterBag($this->getDefaultParameters())); 23 parent::__construct(new FrozenParameterBag($this->getDefaultParameters())); 24 } 25 26 /** 27 * Gets the 'foo' service. 28 * 29 * This service is shared. 30 * This method always returns the same instance of the service. 31 * 32 * @return FooClass A FooClass instance. 33 */ 34 protected function getFooService() 35 { 36 $this->services['foo'] = $instance = new \FooClass(); 37 $instance->setBar('someValue'); 38 39 return $instance; 24 40 } 25 41 … … 33 49 public function findTaggedServiceIds($name) 34 50 { 35 static $tags = array ( 36 ); 51 static $tags = array( 52 53 ); 37 54 38 55 return isset($tags[$name]) ? $tags[$name] : array(); … … 47 64 { 48 65 return array( 49 'foo' => '%baz%', 50 'baz' => 'bar', 51 'bar' => 'foo is %%foo bar', 52 'values' => array( 53 0 => true, 54 1 => false, 55 2 => NULL, 56 3 => 0, 57 4 => 1000.3, 58 5 => 'true', 59 6 => 'false', 60 7 => 'null', 61 ), 66 'cla' => 'Fo', 67 'ss' => 'Class', 62 68 ); 63 69 } branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services_interfaces-1.php
- Property svn:executable set to *
r31486 r31585 25 25 26 26 /** 27 * Gets the 'foo' service. 28 * 29 * This service is shared. 30 * This method always returns the same instance of the service. 31 * 32 * @return Object A %cla%o%ss% instance. 33 */ 34 protected function getFooService() 35 { 36 $class = $this->getParameter('cla').'o'.$this->getParameter('ss'); 37 return $this->services['foo'] = new $class(); 38 39 $this->applyInterfaceInjection($instance); 40 } 41 42 /** 27 43 * Returns service ids for a given tag. 28 44 * … … 33 49 public function findTaggedServiceIds($name) 34 50 { 35 static $tags = array ( 36 ); 51 static $tags = array( 52 53 ); 37 54 38 55 return isset($tags[$name]) ? $tags[$name] : array(); … … 47 64 { 48 65 return array( 49 'foo' => '%baz%', 50 'baz' => 'bar', 51 'bar' => 'foo is %%foo bar', 52 'values' => array( 53 0 => true, 54 1 => false, 55 2 => NULL, 56 3 => 0, 57 4 => 1000.3, 58 5 => 'true', 59 6 => 'false', 60 7 => 'null', 61 ), 66 'cla' => 'Fo', 67 'ss' => 'Class', 62 68 ); 63 69 } 70 71 /** 72 * Applies all known interface injection calls 73 * 74 * @param Object $instance 75 */ 76 protected function applyIntrefaceInjectors($instance) 77 { 78 if ($instance instanceof \FooClass) { 79 $instance->setBar('someValue'); 80 } 81 } 64 82 } branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Loader/XmlFileLoaderTest.php
r31229 r31585 27 27 { 28 28 self::$fixturesPath = realpath(__DIR__.'/../Fixtures/'); 29 require_once self::$fixturesPath.'/includes/foo.php'; 29 30 require_once self::$fixturesPath.'/includes/ProjectExtension.php'; 30 31 require_once self::$fixturesPath.'/includes/ProjectWithXsdExtension.php'; … … 246 247 $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable'); 247 248 } 249 250 public function testLoadInterfaceInjectors() 251 { 252 $container = new ContainerBuilder(); 253 $loader = new ProjectLoader2($container, self::$fixturesPath.'/xml'); 254 $loader->load('interfaces1.xml'); 255 $interfaces = $container->getInterfaceInjectors('FooClass'); 256 $this->assertEquals(1, count($interfaces), '->load() parses <interface> elements'); 257 $interface = $interfaces['FooClass']; 258 $this->assertTrue($interface->hasMethodCall('setBar'), '->load() applies method calls correctly'); 259 } 248 260 } 249 261 branches/2.0/tests/Symfony/Tests/Component/DependencyInjection/Loader/YamlFileLoaderTest.php
r30721 r31585 27 27 { 28 28 self::$fixturesPath = realpath(__DIR__.'/../Fixtures/'); 29 require_once self::$fixturesPath.'/includes/foo.php'; 29 30 require_once self::$fixturesPath.'/includes/ProjectExtension.php'; 30 31 } … … 156 157 $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable'); 157 158 } 159 160 public function testLoadInterfaceInjectors() 161 { 162 $container = new ContainerBuilder(); 163 $loader = new ProjectLoader3($container, self::$fixturesPath.'/yaml'); 164 $loader->load('interfaces1.yml'); 165 $interfaces = $container->getInterfaceInjectors('FooClass'); 166 $this->assertEquals(1, count($interfaces), '->load() parses interfaces'); 167 $interface = $interfaces['FooClass']; 168 $this->assertTrue($interface->hasMethodCall('setBar'), '->load() parses interfaces elements'); 169 } 158 170 } 159 171 branches/2.0/tests/Symfony/Tests/Component/Yaml/Fixtures/sfTests.yml
r30721 r31585 144 144 php: | 145 145 array('foo' => "0123\n") 146 --- 147 test: Document as a simple hash 148 brief: Document as a simple hash 149 yaml: | 150 { foo: bar } 151 php: | 152 array('foo' => 'bar') 153 --- 154 test: Document as a simple array 155 brief: Document as a simple array 156 yaml: | 157 [ foo, bar ] 158 php: | 159 array('foo', 'bar')