-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Modernize and improve DI chapter (#5254)
* [TASK] Modernize and improve DI chapter A major rewrite of the DI chapter, looking at more details, considering more use cases, giving better best practices and adapting to recent core changes. Resolves: #2339 Resolves: #4539 Resolves: #4603 Resolves: #5118 * Update Documentation/ApiOverview/DependencyInjection/Index.rst * Update Documentation/ApiOverview/DependencyInjection/Index.rst * [TASK] Admonition refinement, index refinement, re-add Services.php info --------- Co-authored-by: Garvin Hicking <[email protected]> Co-authored-by: Garvin Hicking <[email protected]>
- Loading branch information
1 parent
72c93e1
commit 82ff036
Showing
24 changed files
with
704 additions
and
371 deletions.
There are no files selected for viewing
768 changes: 478 additions & 290 deletions
768
Documentation/ApiOverview/DependencyInjection/Index.rst
Large diffs are not rendered by default.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
Documentation/ApiOverview/DependencyInjection/_CoreExtensionConfiguration.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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TYPO3\CMS\Core\Configuration; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\AsAlias; | ||
|
||
#[AsAlias('extension-configuration', public: true)] | ||
class ExtensionConfiguration | ||
{ | ||
public function get(string $extension, string $path = ''): mixed | ||
{ | ||
// implementation | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Documentation/ApiOverview/DependencyInjection/_MyDefaultServiceImplementation.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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\AsAlias; | ||
|
||
#[AsAlias(MyServiceInterface::class)] | ||
class MyDefaultServiceImplementation implements MyServiceInterface | ||
{ | ||
public function foo() | ||
{ | ||
// do something | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
Documentation/ApiOverview/DependencyInjection/_MyOtherServiceImplementation.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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
class MyOtherServiceImplementation implements MyServiceInterface | ||
{ | ||
public function foo() | ||
{ | ||
// do something | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
.../ApiOverview/DependencyInjection/_MyServiceGettingExtensionConfigurationValueInjected.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
class MyServiceGettingExtensionConfigurationValueInjected | ||
{ | ||
public function __construct( | ||
#[Autowire(expression: 'service("extension-configuration").get("my_extension", "something.isEnabled")')] | ||
private readonly bool $somethingIsEnabled, | ||
) {} | ||
} |
15 changes: 15 additions & 0 deletions
15
...entation/ApiOverview/DependencyInjection/_MyServiceGettingFeatureToggleResultInjected.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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
|
||
class MyServiceGettingFeatureToggleResultInjected | ||
{ | ||
public function __construct( | ||
#[Autowire(expression: 'service("features").isFeatureEnabled("myExtension.foo")')] | ||
private readonly bool $fooEnabled, | ||
) {} | ||
} |
21 changes: 21 additions & 0 deletions
21
Documentation/ApiOverview/DependencyInjection/_MyServiceGettingRuntimeCacheInjected.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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; | ||
|
||
class MyServiceGettingRuntimeCacheInjected | ||
{ | ||
public function __construct( | ||
#[Autowire(service: 'cache.runtime')] | ||
private readonly FrontendInterface $runtimeCache, | ||
) {} | ||
|
||
public function calculateSomethingExpensive() | ||
{ | ||
// do something using runtime cache | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Documentation/ApiOverview/DependencyInjection/_MyServiceInterface.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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
interface MyServiceInterface | ||
{ | ||
public function foo(); | ||
} |
19 changes: 19 additions & 0 deletions
19
Documentation/ApiOverview/DependencyInjection/_MyServiceUsingAutoconfigurePublicTrue.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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
|
||
/** | ||
* This service is instantiated using GeneralUtility::makeInstance() | ||
* in some cases, which requires 'public' being set to 'true'. | ||
*/ | ||
#[Autoconfigure(public: true)] | ||
readonly class MyServiceUsingAutoconfigurePublicTrue | ||
{ | ||
public function __construct( | ||
private SomeDependency $someDependency, | ||
) {} | ||
} |
26 changes: 26 additions & 0 deletions
26
Documentation/ApiOverview/DependencyInjection/_MyServiceUsingAutoconfigureSharedFalse.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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
|
||
/** | ||
* This service is stateful and configures the service container to | ||
* inject new instances to consuming services when they are instantiated. | ||
*/ | ||
#[Autoconfigure(shared: false)] | ||
class MyServiceUsingAutoconfigureSharedFalse | ||
{ | ||
private string $foo = 'foo'; | ||
|
||
public function __construct( | ||
private readonly SomeDependency $someDependency, | ||
) {} | ||
|
||
public function setFoo(): void | ||
{ | ||
$this->foo = 'bar'; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Documentation/ApiOverview/DependencyInjection/_MyServiceUsingCacheManager.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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Service; | ||
|
||
use TYPO3\CMS\Core\Cache\CacheManager; | ||
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; | ||
|
||
class MyServiceUsingCacheManager | ||
{ | ||
private FrontendInterface $runtimeCache; | ||
|
||
public function __construct( | ||
CacheManager $cacheManager, | ||
) { | ||
$this->runtimeCache = $cacheManager->getCache('runtime'); | ||
} | ||
|
||
public function calculateSomethingExpensive() | ||
{ | ||
// do something using runtime cache | ||
} | ||
} |
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
15 changes: 0 additions & 15 deletions
15
Documentation/ApiOverview/DependencyInjection/_ServicesWithConnection.yaml
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
Documentation/ApiOverview/DependencyInjection/_ServicesWithInterfaceInjection.yaml
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
Documentation/ApiOverview/DependencyInjection/_ServicesWithPublic.yaml
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
Documentation/ApiOverview/DependencyInjection/_ServicesWithoutAutowire.yaml
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
...ation/ApiOverview/DependencyInjection/_ServicesYamlDeclaringForeignServicePublicTrue.yaml
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,15 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
|
||
MyVendor\MyExtension\: | ||
resource: '../Classes/*' | ||
|
||
# Declare a "foreign" service "public: true" since this extension needs | ||
# to instantiate the service using GeneralUtility::makeInstance() and | ||
# the service is configured "public: false" by the extension delivering | ||
# that service. | ||
TYPO3\CMS\Core\Some\Service: | ||
public: true |
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
Documentation/ApiOverview/DependencyInjection/_ServicesYamlInstallationWide.yaml
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,4 @@ | ||
services: | ||
|
||
Psr\Clock\ClockInterface: | ||
factory: ['Lcobucci\Clock\SystemClock', 'fromUTC'] |
21 changes: 21 additions & 0 deletions
21
Documentation/ApiOverview/DependencyInjection/_ServicesYamlUsingInterfaceInjection.yaml
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,21 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
|
||
# Within MySecondController and MyThirdController different implementations | ||
# than the default MyDefaultServiceImplementation of MyServiceInterface | ||
# shall be injected. | ||
|
||
# When working with constructor injection | ||
MyVendor\MyExtension\Controller\MySecondController: | ||
arguments: | ||
$service: '@MyVendor\MyExtension\Service\MyOtherServiceImplementation' | ||
|
||
# When working with method injection | ||
MyVendor\MyExtension\Controller\MyThirdController: | ||
calls: | ||
- method: 'injectMyService' | ||
arguments: | ||
$service: '@MyVendor\MyExtension\Service\MyOtherServiceImplementation' |
17 changes: 0 additions & 17 deletions
17
Documentation/ApiOverview/DependencyInjection/_servicesInstallationWide.php
This file was deleted.
Oops, something went wrong.