Skip to content

Commit 64543db

Browse files
authored
Merge pull request #163 from robbiejackson/di-tidy-up
Some tidy up and previous DI page removed
2 parents 5046255 + f7c4dae commit 64543db

File tree

7 files changed

+18
-194
lines changed

7 files changed

+18
-194
lines changed

docs/general-concept/dependencies.md

Lines changed: 0 additions & 182 deletions
This file was deleted.

docs/general-concept/dependency-injection/DIC.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ It doesn't *really* matter what the key is set to - as long as it's intelligible
1212
![DIC](_assets/dic.jpg "DIC")
1313

1414
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).
1919

2020
The function `share()` is basically the same as `set()` with the shared boolean set to true.
2121

docs/general-concept/dependency-injection/di-issues.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ title: Dependency Injection Issues
33
sidebar_position: 7
44
---
55
# Dependency Injection Issues
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. 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!
79

810
## Toolbar::getInstance()
911
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

docs/general-concept/dependency-injection/extension-child-containers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use Joomla\CMS\Extension\ComponentInterface;
2121
use Joomla\DI\Container;
2222
use Joomla\DI\ServiceProviderInterface;
2323
use Mycompany\Component\Example\Administrator\Extension\ExampleComponent;
24+
2425
return new class implements ServiceProviderInterface {
2526
public function register(Container $container): void
2627
{

docs/general-concept/dependency-injection/jconfig-example.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Or you can obtain them via the Application instance, which has already got them
6666
```php
6767
use Joomla\CMS\Factory;
6868
$application = Factory::getApplication();
69-
$application->getConfig(); // or, to get a particular parameter:
69+
$application->getConfig();
70+
// or, to get a particular parameter:
7071
$application->get($paramName, $defaultValue);
7172
```

docs/general-concept/dependency-injection/modules-and-plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public function register(Container $container)
4747
```
4848
The plugin's Extension class is Color, which gets instantiated with 2 parameters being passed into its constructor:
4949
- 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).
5151

5252
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.

docs/general-concept/dependency-injection/registering-subdependencies.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
1717
use Joomla\DI\Container;
1818
use Joomla\DI\ServiceProviderInterface;
1919
use Mycompany\Component\Example\Administrator\Extension\ExampleComponent;
20+
2021
return new class implements ServiceProviderInterface {
2122
public function register(Container $container): void
2223
{
@@ -61,7 +62,7 @@ Let's look first at the ComponentDispatcherFactory
6162
```php
6263
$component = new MVCComponent($container->get(ComponentDispatcherFactoryInterface::class));
6364
```
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:
6566
```php
6667
new \Joomla\CMS\Dispatcher\ComponentDispatcherFactory($this->namespace, $container->get(MVCFactoryInterface::class))
6768
```
@@ -83,12 +84,12 @@ Let's take another look at the line when the ComponentDispatcherFactory is insta
8384
```php
8485
new \Joomla\CMS\Dispatcher\ComponentDispatcherFactory($this->namespace, $container->get(MVCFactoryInterface::class))
8586
```
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:
8788
```php
8889
$controller = $this->mvcFactory->createController(...);
8990
$controller->execute($task);
9091
```
91-
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.
9293

9394
## Resolving the MVCFactory Dependency
9495
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
104105
$component = new MVCComponent($container->get(ComponentDispatcherFactoryInterface::class));
105106
$component->setMVCFactory($container->get(MVCFactoryInterface::class));
106107
```
107-
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:
108111
```php
109112
public function setMVCFactory(MVCFactoryInterface $mvcFactory)
110113
{
111114
$this->mvcFactory = $mvcFactory;
112115
}
113-
114116
```
115117
## Namespaces parameters
116118
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

Comments
 (0)