You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/general-concept/dependency-injection/DIC.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,10 +12,10 @@ It doesn't *really* matter what the key is set to - as long as it's intelligible
12
12

13
13
14
14
You put things into the container using `set()` passing:
15
-
- the class name or interface name
16
-
- a function which returns an instance of the class (or the value can be just an instance of the class, without the enveloping function)
17
-
- a boolean defining whether the class instance may be shared or not (ie if there is a second request to the DIC to supply that instance, does it return the same instance or a new one)
18
-
- a boolean defining whether this entry into the DI container is protected or not (an error will be raised if you try to overwrite a protected entry by calling `set()` again using the same key).
15
+
- the key = the class name or interface name
16
+
-the value = a function which returns an instance of the class (or the value can be just an instance of the class, without the enveloping function)
17
+
-shared - a boolean defining whether the class instance may be shared or not (ie if there is a second request to the DIC to supply that instance, does it return the same instance or a new one)
18
+
-protected - a boolean defining whether this entry into the DI container is protected or not (an error will be raised if you try to overwrite a protected entry by calling `set()` again using the same key).
19
19
20
20
The function `share()` is basically the same as `set()` with the shared boolean set to true.
Dependency Injection was introduced in Joomla 4, with the intention of removing direct access to most key classes via `getInstance()` calls, including via `Factory::` calls, and many of these API calls are deprecated in Joomla 4. However, not all `getInstance()` calls are deprecated; examples which are not deprecated include `Uri::getInstance()` and `Filter\InputFilter::getInstance()`.
6
+
Dependency Injection was introduced in Joomla 4, with the intention of removing direct access to most key classes via `getInstance()` calls, including via `Factory::` calls, and many of these API calls are deprecated in Joomla 4. Note that not all `getInstance()` calls are deprecated; examples which are not deprecated include `Uri::getInstance()` and `Filter\InputFilter::getInstance()`.
7
+
8
+
However, some issues do remain, which you should be aware of. Early versions of Joomla 5 still have a lot of these `getInstance` calls!
7
9
8
10
## Toolbar::getInstance()
9
11
The `Toolbar` class is used for managing the buttons on the forms within the Joomla administrator back-end. The [Toolbar API](https://api.joomla.org/cms-4/classes/Joomla-CMS-Toolbar-Toolbar.html) documentation seems to suggest that we should replace
Copy file name to clipboardExpand all lines: docs/general-concept/dependency-injection/modules-and-plugins.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,6 +47,6 @@ public function register(Container $container)
47
47
```
48
48
The plugin's Extension class is Color, which gets instantiated with 2 parameters being passed into its constructor:
49
49
- the EventDispatcher class – this is obtained from the parent DIC, having been originally put into it from libraries/src/Service/Provider/Dispatcher.php when Joomla initialised
50
-
- the plugin stdClass object which Joomla has used to store plugin data (id, name, type and params).
50
+
- the plugin stdClass object which Joomla has traditionally used to store plugin data (id, name, type and params).
51
51
52
52
This is a standard pattern that you can use for your own plugins. You can obviously get the Application instance by calling `Factory::getApplication` either in the services/provider.php file or in your standard plugin code.
Copy file name to clipboardExpand all lines: docs/general-concept/dependency-injection/registering-subdependencies.md
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@ use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
17
17
use Joomla\DI\Container;
18
18
use Joomla\DI\ServiceProviderInterface;
19
19
use Mycompany\Component\Example\Administrator\Extension\ExampleComponent;
20
+
20
21
return new class implements ServiceProviderInterface {
21
22
public function register(Container $container): void
22
23
{
@@ -61,7 +62,7 @@ Let's look first at the ComponentDispatcherFactory
61
62
```php
62
63
$component = new MVCComponent($container->get(ComponentDispatcherFactoryInterface::class));
63
64
```
64
-
The code `$container->get(ComponentDispatcherFactoryInterface::class)` runs the function in the child DIC associated with the ComponentDispatcherFactoryInterface::class key, and the object returned:
65
+
The code `$container->get(ComponentDispatcherFactoryInterface::class)` runs the function in the child DIC associated with the ComponentDispatcherFactoryInterface::class key. This function is found in libraries/src/Extension/Service/Provider/ComponentDispatcherFactory.php, and the object returned:
65
66
```php
66
67
new \Joomla\CMS\Dispatcher\ComponentDispatcherFactory($this->namespace, $container->get(MVCFactoryInterface::class))
67
68
```
@@ -83,12 +84,12 @@ Let's take another look at the line when the ComponentDispatcherFactory is insta
83
84
```php
84
85
new \Joomla\CMS\Dispatcher\ComponentDispatcherFactory($this->namespace, $container->get(MVCFactoryInterface::class))
85
86
```
86
-
Here the code is also getting the MVCFactory instance out of the child DIC; the ComponentDispatcherFactory has in turn a dependency on the MVCFactory. Why is this? As described in the [Dispatcher document](../extension-and-dispatcher/dispatcher-component.md) whenever the Dispatcher's `dispatch` function is called it analyses the *task* parameter of the URL, to decide which Controller class to instantiate. So it stores the MVCFactory object passed in its constructor so that it can later use it to create the Controller class:
87
+
Here the code is also getting the MVCFactory instance out of the child DIC; the ComponentDispatcherFactory has in turn a dependency on the MVCFactory. Why is this? As described in the [Dispatcher documentation](../extension-and-dispatcher/dispatcher-component.md) whenever the Dispatcher's `dispatch` function is called it analyses the *task* parameter of the URL, to decide which Controller class to instantiate. So it stores the MVCFactory object passed in its constructor so that it can later use it to create the Controller class:
So the ComponentDispatcherFactory gets MVCFactory out of the DIC, stores a reference to it, and then passes it down to the Dispatcher class it instantiates it. Its code is in libraries/src/Dispatcher/ComponentDispatcherFactory.php.
92
+
So the ComponentDispatcherFactory gets MVCFactory out of the DIC, stores a reference to it, and then passes it down to the Dispatcher class when instantiates it. Its code is in libraries/src/Dispatcher/ComponentDispatcherFactory.php.
92
93
93
94
## Resolving the MVCFactory Dependency
94
95
This follows a pattern similar to that of the ComponentDispatcherFactory dependency.
@@ -104,13 +105,14 @@ The second step involves instantiating the Extension class and resolving its dep
104
105
$component = new MVCComponent($container->get(ComponentDispatcherFactoryInterface::class));
The code `$container->get(MVCFactoryInterface::class` obtains the MVCFactory instance from the child DIC, and then a reference to it is stored locally through it being passed to `setMVCFactory`. This function is available to `MVCComponent` via the trait Joomla\CMS\MVC\Factory\MVCFactoryServiceTrait, which contains the lines:
108
+
In the first line the Extension class (MVCComponent) gets instantiated, with the ComponentDispatchFactory instance being passed into the constructor (as we saw above).
109
+
110
+
In the second line the code `$container->get(MVCFactoryInterface::class` obtains the MVCFactory instance from the child DIC, and then a reference to it is stored locally through it being passed to `setMVCFactory`. This function is available to `MVCComponent` via the trait Joomla\CMS\MVC\Factory\MVCFactoryServiceTrait, which is used in MVCComponent, and which contains the lines:
108
111
```php
109
112
public function setMVCFactory(MVCFactoryInterface $mvcFactory)
110
113
{
111
114
$this->mvcFactory = $mvcFactory;
112
115
}
113
-
114
116
```
115
117
## Namespaces parameters
116
118
You will have noticed that the namespace '\Mycompany\Component\Example' gets passed as a parameter into several classes constructors. (By the way, this is exactly the same PHP string as its equivalent with double backslashes instead of single backslashes).
0 commit comments