forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DependencyInjection] Explicitly add service definitions with a new `…
…Service` attribute Sometimes its better to explicitly mark classes that should be service definitions instead of including everything and then working with either the `exclude` path from the resource loader, or the `Exclude` attribute. To achieve this, the following things were done: * Add a new `Service` attribute which can be used on classes * Add a new `onlyWithServiceAttribute` configuration to the resource loader If this option (along with the `autoconfigure=true`) is set to `true`, only classes that have the `Service` attribute will be added to the service definition in the container. All other classes/files from the resource path will still be added, but marked as excluded with the `container.excluded` tag. Example PHP configuration: ```php return function (ContainerConfigurator $c) { $services = $c->services(); $services ->load('YourNamespace\\', '/path/to/namespace') ->onlyWithServiceAttribute() ->autoconfigure(); }; ``` Example XML configuration: ```xml <?xml version="1.0" encoding="utf-8"?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <prototype namespace="YourNamespace\" resource="/path/to/namespace" autoconfigure="true" onlyWithServiceAttribute="true" /> </services> </container> ``` Example YML configuration: ```yml services: YourNamespace\: resource: /path/to/namespace onlyWithServiceAttribute: true autoconfigure: true ```
- Loading branch information
Showing
22 changed files
with
278 additions
and
14 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/Symfony/Component/DependencyInjection/Attribute/Service.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Attribute; | ||
|
||
/** | ||
* An attribute to explicitly mark a class as a service definition. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class Service | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...mponent/DependencyInjection/Tests/Fixtures/Prototype/ServiceAttributes/BarWithService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\ServiceAttributes; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Service; | ||
|
||
#[Service] | ||
class BarWithService | ||
{ | ||
public function __construct($bar = null, ?iterable $foo = null, ?object $baz = null) | ||
{ | ||
} | ||
|
||
public function setFoo(self $foo) | ||
{ | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...mponent/DependencyInjection/Tests/Fixtures/Prototype/ServiceAttributes/FooWithService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\ServiceAttributes; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Service; | ||
|
||
#[Service] | ||
class FooWithService | ||
{ | ||
public function __construct($bar = null, ?iterable $foo = null, ?object $baz = null) | ||
{ | ||
} | ||
|
||
public function setFoo(self $foo) | ||
{ | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...nent/DependencyInjection/Tests/Fixtures/Prototype/ServiceAttributes/FooWithoutService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\ServiceAttributes; | ||
|
||
class FooWithoutService | ||
{ | ||
public function __construct($bar = null, ?iterable $foo = null, ?object $baz = null) | ||
{ | ||
} | ||
|
||
public function setFoo(self $foo) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...onent/DependencyInjection/Tests/Fixtures/config/prototype_service_attributes.expected.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
services: | ||
service_container: | ||
class: Symfony\Component\DependencyInjection\ContainerInterface | ||
public: true | ||
synthetic: true | ||
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\ServiceAttributes\BarWithService: | ||
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\ServiceAttributes\BarWithService | ||
public: true | ||
tags: | ||
- foo | ||
- baz | ||
deprecated: | ||
package: vendor/package | ||
version: '1.1' | ||
message: '%service_id%' | ||
autoconfigure: true | ||
lazy: true | ||
arguments: [1] | ||
factory: f | ||
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\ServiceAttributes\FooWithService: | ||
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\ServiceAttributes\FooWithService | ||
public: true | ||
tags: | ||
- foo | ||
- baz | ||
deprecated: | ||
package: vendor/package | ||
version: '1.1' | ||
message: '%service_id%' | ||
autoconfigure: true | ||
lazy: true | ||
arguments: [1] | ||
factory: f |
24 changes: 24 additions & 0 deletions
24
...fony/Component/DependencyInjection/Tests/Fixtures/config/prototype_service_attributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype; | ||
|
||
return function (ContainerConfigurator $c) { | ||
$di = $c->services()->defaults() | ||
->tag('baz'); | ||
|
||
$di->load(Prototype::class.'\\', '../Prototype') | ||
->onlyWithServiceAttribute() | ||
->public() | ||
->autoconfigure() | ||
->exclude('../Prototype/{BadClasses,BadAttributes}') | ||
->factory('f') | ||
->deprecate('vendor/package', '1.1', '%service_id%') | ||
->args([0]) | ||
->args([1]) | ||
->tag('foo') | ||
->parent('foo'); | ||
|
||
$di->set('foo')->lazy()->abstract()->public(); | ||
}; |
2 changes: 1 addition & 1 deletion
2
src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_prototype.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" exclude="../Prototype/{OtherDir,BadClasses,BadAttributes,SinglyImplementedInterface,StaticConstructor}" /> | ||
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" exclude="../Prototype/{OtherDir,BadClasses,BadAttributes,SinglyImplementedInterface,StaticConstructor,ServiceAttributes}" /> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...DependencyInjection/Tests/Fixtures/xml/services_prototype_only_with_service_attribute.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<prototype namespace="Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\" resource="../Prototype/*" exclude="../Prototype/{BadClasses,BadAttributes}" autoconfigure="true" onlyWithServiceAttribute="true" /> | ||
</services> | ||
</container> |
2 changes: 1 addition & 1 deletion
2
src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_prototype.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
services: | ||
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\: | ||
resource: ../Prototype | ||
exclude: '../Prototype/{OtherDir,BadClasses,BadAttributes,SinglyImplementedInterface,StaticConstructor}' | ||
exclude: '../Prototype/{OtherDir,BadClasses,BadAttributes,SinglyImplementedInterface,StaticConstructor,ServiceAttributes}' |
6 changes: 6 additions & 0 deletions
6
...ependencyInjection/Tests/Fixtures/yaml/services_prototype_only_with_service_attribute.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\: | ||
resource: ../Prototype | ||
exclude: '../Prototype/{BadClasses,BadAttributes}' | ||
onlyWithServiceAttribute: true | ||
autoconfigure: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.