diff --git a/components/dependency_injection/advanced.rst b/components/dependency_injection/advanced.rst index 77056c296ce..2d07d0eab13 100644 --- a/components/dependency_injection/advanced.rst +++ b/components/dependency_injection/advanced.rst @@ -42,10 +42,20 @@ Here is an example: .. code-block:: xml - + + + + + + + .. code-block:: php + use Symfony\Component\DependencyInjection\Definition; + $definition = new Definition('Example\Foo'); $definition->setPublic(false); $container->setDefinition('foo', $definition); @@ -88,15 +98,22 @@ To create a synthetic service, set ``synthetic`` to ``true``: .. code-block:: xml - + + + + + + + .. code-block:: php use Symfony\Component\DependencyInjection\Definition; - // ... - $container->setDefinition('request', new Definition()) + $container + ->setDefinition('request', new Definition()) ->setSynthetic(true); As you see, only the ``synthetic`` option is set. All other options are only used @@ -128,14 +145,23 @@ services. .. code-block:: xml - + + + + + - + + + .. code-block:: php - $definition = new Definition('Example\Foo'); - $container->setDefinition('foo', $definition); + use Symfony\Component\DependencyInjection\Definition; + + $container->setDefinition('foo', new Definition('Example\Foo')); $containerBuilder->setAlias('bar', 'foo'); @@ -173,12 +199,22 @@ the service itself gets loaded. To do so, you can use the ``file`` directive. .. code-block:: xml - - %kernel.root_dir%/src/path/to/file/foo.php - + + + + + + %kernel.root_dir%/src/path/to/file/foo.php + + + .. code-block:: php + use Symfony\Component\DependencyInjection\Definition; + $definition = new Definition('Example\Foo\Bar'); $definition->setFile('%kernel.root_dir%/src/path/to/file/foo.php'); $container->setDefinition('foo', $definition); diff --git a/components/dependency_injection/compilation.rst b/components/dependency_injection/compilation.rst index 9acbdde0aad..84b4afe9b95 100644 --- a/components/dependency_injection/compilation.rst +++ b/components/dependency_injection/compilation.rst @@ -203,7 +203,6 @@ The XML version of the config would then look like this: fooValue barValue - .. note:: diff --git a/components/dependency_injection/configurators.rst b/components/dependency_injection/configurators.rst index d13c9118fb6..c8036fc8685 100644 --- a/components/dependency_injection/configurators.rst +++ b/components/dependency_injection/configurators.rst @@ -155,30 +155,40 @@ The service config for the above classes would look something like this: .. code-block:: xml - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + .. code-block:: php diff --git a/components/dependency_injection/factories.rst b/components/dependency_injection/factories.rst index 96df6429244..0682a00e624 100644 --- a/components/dependency_injection/factories.rst +++ b/components/dependency_injection/factories.rst @@ -47,18 +47,24 @@ class: .. code-block:: xml - - - NewsletterManager - NewsletterFactory - - - - + + + + + + NewsletterManager + NewsletterFactory + + + + + .. code-block:: php @@ -69,13 +75,11 @@ class: $container->setParameter('newsletter_manager.class', 'NewsletterManager'); $container->setParameter('newsletter_factory.class', 'NewsletterFactory'); - $container->setDefinition('newsletter_manager', new Definition( - '%newsletter_manager.class%' - ))->setFactoryClass( - '%newsletter_factory.class%' - )->setFactoryMethod( - 'get' - ); + $definition = new Definition('%newsletter_manager.class%'); + $definition->setFactoryClass('%newsletter_factory.class%'); + $definition->setFactoryMethod('get'); + + $container->setDefinition('newsletter_manager', $definition); When you specify the class to use for the factory (via ``factory_class``) the method will be called statically. If the factory itself should be instantiated @@ -100,20 +104,27 @@ In this case, the method (e.g. get) should be changed to be non-static: .. code-block:: xml - - - NewsletterManager - NewsletterFactory - - - - - - + + + + + + NewsletterManager + NewsletterFactory + + + + + + + + .. code-block:: php @@ -137,7 +148,8 @@ In this case, the method (e.g. get) should be changed to be non-static: .. note:: The factory service is specified by its id name and not a reference to - the service itself. So, you do not need to use the @ syntax. + the service itself. So, you do not need to use the @ syntax for this in + YAML configurations. Passing Arguments to the Factory Method --------------------------------------- @@ -166,22 +178,30 @@ in the previous example takes the ``templating`` service as an argument: .. code-block:: xml - - - NewsletterManager - NewsletterFactory - - - - - - - - + + + + + + NewsletterManager + NewsletterFactory + + + + + + + + + + + .. code-block:: php diff --git a/components/dependency_injection/introduction.rst b/components/dependency_injection/introduction.rst index 0d1448a1cfa..50d5d2d19c5 100644 --- a/components/dependency_injection/introduction.rst +++ b/components/dependency_injection/introduction.rst @@ -244,22 +244,28 @@ config files: .. code-block:: xml - - - sendmail - - - - - %mailer.transport% - - - - - - - - + + + + + + sendmail + + + + + %mailer.transport% + + + + + + + + + .. code-block:: php diff --git a/components/dependency_injection/lazy_services.rst b/components/dependency_injection/lazy_services.rst index 39f5d3da22f..57226d9e463 100644 --- a/components/dependency_injection/lazy_services.rst +++ b/components/dependency_injection/lazy_services.rst @@ -62,10 +62,20 @@ You can mark the service as ``lazy`` by manipulating its definition: .. code-block:: xml - + + + + + + + .. code-block:: php + use Symfony\Component\DependencyInjection\Definition; + $definition = new Definition('Acme\Foo'); $definition->setLazy(true); $container->setDefinition('foo', $definition); diff --git a/components/dependency_injection/parameters.rst b/components/dependency_injection/parameters.rst index e30a186fb76..86cf5a489eb 100644 --- a/components/dependency_injection/parameters.rst +++ b/components/dependency_injection/parameters.rst @@ -52,9 +52,15 @@ You can also use the ``parameters`` section of a config file to set parameters: .. code-block:: xml - - sendmail - + + + + + sendmail + + .. code-block:: php @@ -84,22 +90,28 @@ rather than being tied up and hidden with the service definition: .. code-block:: xml - - sendmail - + + - - - %mailer.transport% - - + + sendmail + + + + + %mailer.transport% + + + .. code-block:: php use Symfony\Component\DependencyInjection\Reference; - // ... $container->setParameter('mailer.transport', 'sendmail'); + $container ->register('mailer', 'Mailer') ->addArgument('%mailer.transport%'); @@ -146,32 +158,34 @@ making the class of a service a parameter: .. code-block:: xml - - sendmail - Mailer - + + + + + sendmail + Mailer + - - - %mailer.transport% - - + + + %mailer.transport% + + + .. code-block:: php use Symfony\Component\DependencyInjection\Reference; - // ... $container->setParameter('mailer.transport', 'sendmail'); $container->setParameter('mailer.class', 'Mailer'); + $container ->register('mailer', '%mailer.class%') ->addArgument('%mailer.transport%'); - $container - ->register('newsletter_manager', 'NewsletterManager') - ->addMethodCall('setMailer', array(new Reference('mailer'))); - .. note:: The percent sign inside a parameter or argument, as part of the string, must @@ -181,11 +195,11 @@ making the class of a service a parameter: .. code-block:: yaml - arguments: ['http://symfony.com/?foo=%%s&bar=%%d'] + arguments: ["http://symfony.com/?foo=%%s&bar=%%d"] .. code-block:: xml - http://symfony.com/?foo=%%s&bar=%%d + http://symfony.com/?foo=%%s&bar=%%d .. code-block:: php @@ -204,7 +218,6 @@ all parameters that are arrays. .. code-block:: yaml - # app/config/config.yml parameters: my_mailer.gateways: - mail1 @@ -220,30 +233,32 @@ all parameters that are arrays. .. code-block:: xml - - - - mail1 - mail2 - mail3 - - - - en - fr + + + + + + mail1 + mail2 + mail3 - - fr - en + + + en + fr + + + fr + en + - - + + .. code-block:: php - // app/config/config.php - use Symfony\Component\DependencyInjection\Definition; - $container->setParameter('my_mailer.gateways', array('mail1', 'mail2', 'mail3')); $container->setParameter('my_multilang.language_fallback', array( 'en' => array('en', 'fr'), @@ -263,10 +278,10 @@ key, and define the type as ``constant``. .. code-block:: xml - - + + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> GLOBAL_CONSTANT @@ -276,21 +291,18 @@ key, and define the type as ``constant``. .. code-block:: php - $container->setParameter('global.constant.value', GLOBAL_CONSTANT); - $container->setParameter('my_class.constant.value', My_Class::CONSTANT_NAME); + $container->setParameter('global.constant.value', GLOBAL_CONSTANT); + $container->setParameter('my_class.constant.value', My_Class::CONSTANT_NAME); .. note:: - This does not work for YAML configuration. If you're using YAML, you can - import an XML file to take advantage of this functionality: + This does not work for YAML configurations. If you're using YAML, you + can import an XML file to take advantage of this functionality: - .. configuration-block:: - - .. code-block:: yaml + .. code-block:: yaml - # app/config/config.yml - imports: - - { resource: parameters.xml } + imports: + - { resource: parameters.xml } PHP Keywords in XML ------------------- diff --git a/components/dependency_injection/parentservices.rst b/components/dependency_injection/parentservices.rst index 8b3cf6fb7e3..d8d53cffdbd 100644 --- a/components/dependency_injection/parentservices.rst +++ b/components/dependency_injection/parentservices.rst @@ -78,40 +78,46 @@ The service config for these classes would look something like this: .. code-block:: xml - - - NewsletterManager - GreetingCardManager - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + NewsletterManager + GreetingCardManager + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + .. code-block:: php @@ -211,29 +217,35 @@ a parent for a service. .. code-block:: xml - - + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + .. code-block:: php @@ -242,7 +254,6 @@ a parent for a service. use Symfony\Component\DependencyInjection\Reference; // ... - $mailManager = new Definition(); $mailManager ->setAbstract(true); @@ -336,38 +347,44 @@ to the ``NewsletterManager`` class, the config would look like this: .. code-block:: xml - + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + .. code-block:: php diff --git a/components/dependency_injection/tags.rst b/components/dependency_injection/tags.rst index 67a4a730cce..ab523a10a77 100644 --- a/components/dependency_injection/tags.rst +++ b/components/dependency_injection/tags.rst @@ -48,13 +48,19 @@ Then, define the chain as a service: .. code-block:: xml - - TransportChain - + + - - - + + TransportChain + + + + + + .. code-block:: php @@ -89,14 +95,22 @@ For example you may add the following transports as services: .. code-block:: xml - - %mailer_host% - - + + + + + + %mailer_host% + + - - - + + + + + .. code-block:: php @@ -226,14 +240,22 @@ To answer this, change the service declaration: .. code-block:: xml - - %mailer_host% - - - - - - + + + + + + %mailer_host% + + + + + + + + .. code-block:: php diff --git a/components/dependency_injection/types.rst b/components/dependency_injection/types.rst index 5efc223c766..3fb9181fdea 100644 --- a/components/dependency_injection/types.rst +++ b/components/dependency_injection/types.rst @@ -47,21 +47,27 @@ service container configuration: .. code-block:: xml - - - - - - - - + + + + + + + + + + + + + .. code-block:: php use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; - // ... $container->setDefinition('my_mailer', ...); $container->setDefinition('newsletter_manager', new Definition( 'NewsletterManager', @@ -123,23 +129,29 @@ accepts the dependency:: .. code-block:: xml - - - - - - - - - - + + + + + + + + + + + + + + + .. code-block:: php use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; - // ... $container->setDefinition('my_mailer', ...); $container->setDefinition('newsletter_manager', new Definition( 'NewsletterManager' @@ -190,25 +202,31 @@ Another possibility is just setting public fields of the class directly:: .. code-block:: xml - - - - - - - - + + + + + + + + + + + + + .. code-block:: php use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; - // ... $container->setDefinition('my_mailer', ...); $container->setDefinition('newsletter_manager', new Definition( 'NewsletterManager' - ))->setProperty('mailer', new Reference('my_mailer'))); + ))->setProperty('mailer', new Reference('my_mailer')); There are mainly only disadvantages to using property injection, it is similar to setter injection but with these additional important problems: diff --git a/components/routing/hostname_pattern.rst b/components/routing/hostname_pattern.rst index b3052f66a74..fd2e8671504 100644 --- a/components/routing/hostname_pattern.rst +++ b/components/routing/hostname_pattern.rst @@ -25,12 +25,10 @@ You can also match on the HTTP *host* of the incoming request. .. code-block:: xml - + http://symfony.com/schema/routing/routing-1.0.xsd"> AcmeDemoBundle:Main:mobileHomepage @@ -82,12 +80,10 @@ you can use placeholders in your hostname: .. code-block:: xml - + http://symfony.com/schema/routing/routing-1.0.xsd"> AcmeDemoBundle:Main:mobileHomepage @@ -138,17 +134,14 @@ instance, if you want to match both ``m.example.com`` and .. code-block:: xml - + http://symfony.com/schema/routing/routing-1.0.xsd"> AcmeDemoBundle:Main:mobileHomepage m - m|mobile @@ -201,7 +194,6 @@ instance, if you want to match both ``m.example.com`` and .. code-block:: xml - @@ -253,16 +245,13 @@ You can also set the host option on imported routes: .. code-block:: yaml - # app/config/routing.yml acme_hello: resource: "@AcmeHelloBundle/Resources/config/routing.yml" host: "hello.example.com" .. code-block:: xml - - @@ -272,7 +261,6 @@ You can also set the host option on imported routes: .. code-block:: php - // app/config/routing.php use Symfony\Component\Routing\RouteCollection; $collection = new RouteCollection();