Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improvements to registering an extension (#2)#3236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
weaverryan merged 2 commits intosymfony:2.2fromflip111:patch-2
Dec 23, 2013
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 45 additions & 41 deletionscookbook/bundles/extension.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -177,6 +177,51 @@ You can begin specifying configuration under this namespace immediately:
array. You can still provide some sensible defaults for your bundle if
you want.

Registering the Extension Class
-------------------------------

An Extension class will automatically be registered by Symfony2 when
following these simple conventions:

* The extension must be stored in the ``DependencyInjection`` sub-namespace;

* The extension must be named after the bundle name and suffixed with
``Extension`` (``AcmeHelloExtension`` for ``AcmeHelloBundle``);

* The extension should provide an XSD schema.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Not sure if this is correct, I'll investigate it tomorrow

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It's not mandatory but good practive if your bundle provides its own config tree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

That means it should not be put in this list

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is outside of the scope of this PR (see improvements points 1,2 and 3). Please submit a new PR for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I see no reason not to do such small change in this PR?


Manually Registering an Extension Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When not following the conventions you will have to manually register your
extension. To manually register an extension class override the
:method:`Bundle::build() <Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

:method:`Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build`

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is outside of the scope of this PR (see improvements points 1,2 and 3). Please submit a new PR for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

No it is not

method in your bundle::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

method in your bundle (e.g. when the extension does not follow the conventions)::

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I belief you can follow the conventions and still manual register your extension.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, that's why I added "e.g." as an example why you would want to do that. Btw, I see no reason why you want to register it when it already would get registered by Symfony?


// ...
use Acme\HelloBundle\DependencyInjection\UnconventionalExtensionClass;

class AcmeHelloBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

// register extensions that do not follow the conventions manually
$container->registerExtension(new UnconventionalExtensionClass());
}
}

In this case, the extension class must also implement a ``getAlias()`` method
and return a unique alias named after the bundle (e.g. ``acme_hello``). This
is required because the class name doesn't follow the conventions by ending
in ``Extension``.

Additionally, the ``load()`` method of your extension will *only* be called
if the user specifies the ``acme_hello`` alias in at least one configuration
file. Once again, this is because the Extension class doesn't follow the
conventions set out above, so nothing happens automatically.

Parsing the ``$configs`` Array
------------------------------

Expand DownExpand Up@@ -561,46 +606,5 @@ command.
.. index::
pair: Convention; Configuration

Extension Conventions
---------------------

When creating an extension, follow these simple conventions:

* The extension must be stored in the ``DependencyInjection`` sub-namespace;

* The extension must be named after the bundle name and suffixed with
``Extension`` (``AcmeHelloExtension`` for ``AcmeHelloBundle``);

* The extension should provide an XSD schema.

If you follow these simple conventions, your extensions will be registered
automatically by Symfony2. If not, override the
:method:`Bundle::build() <Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build>`
method in your bundle::

// ...
use Acme\HelloBundle\DependencyInjection\UnconventionalExtensionClass;

class AcmeHelloBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

// register extensions that do not follow the conventions manually
$container->registerExtension(new UnconventionalExtensionClass());
}
}

In this case, the extension class must also implement a ``getAlias()`` method
and return a unique alias named after the bundle (e.g. ``acme_hello``). This
is required because the class name doesn't follow the standards by ending
in ``Extension``.

Additionally, the ``load()`` method of your extension will *only* be called
if the user specifies the ``acme_hello`` alias in at least one configuration
file. Once again, this is because the Extension class doesn't follow the
standards set out above, so nothing happens automatically.

.. _`FrameworkBundle Configuration`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
.. _`TwigBundle Configuration`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

[8]ページ先頭

©2009-2025 Movatter.jp