@@ -12,7 +12,7 @@ support the goals of dependency injection. For example Symfony
1212[ introduced the concept
1313in 2009] ( http://fabien.potencier.org/do-you-need-a-dependency-injection-container.html ) .
1414
15- There's a variety of reasons why now is the right time to introduce
15+ There's a variety of reasons why it is the right time to introduce
1616these into Joomla 4:
1717
18181 . ** Testing** - one of the themes of Joomla 3 has been buggy releases.
@@ -34,25 +34,25 @@ replacement.
3434
3535So for example in your Controllers in the CMS instead of substituting
3636
37- ``` php
37+ ``` php
3838\Joomla\CMS\Factory::getDocument()
3939```
4040
4141consider using
4242
43- ``` php
43+ ``` php
4444$this->app->getDocument()
4545```
4646
47- . This uses the injected application and therefore allows for easier
47+ This uses the injected application and therefore allows for easier
4848testing.
4949
5050### Creating an object in a container
5151
5252To place something in the Global DIC the most simple way is to pass in
5353an anonymous function. An example for a logger is below
5454
55- ``` php
55+ ``` php
5656// Assuming we have an instance of a Joomla Container
5757$container->share(
5858 LoggerInterface::class,
@@ -81,7 +81,7 @@ parameter.
8181Let's now look at a more complicated
8282example:
8383
84- ``` php
84+ ``` php
8585$container->alias('AmazingApiRouter', Joomla\CMS\Router\ApiRouter::class)
8686 ->share(
8787 \Joomla\CMS\Router\ApiRouter::class,
@@ -99,7 +99,7 @@ and we've also created an alias for the ApiRouter. That means whilst the
9999container recognises that if it needs to build an ApiRouter instance it
100100can do that. But in our code to keep things simple we can also run
101101
102- ``` php
102+ ``` php
103103Factory::getContainer()->get('AmazingApiRouter')
104104```
105105
@@ -114,55 +114,69 @@ more complicated - all of them follow this base idea.
114114Providers in Joomla are a way of registering a dependency into a service
115115container. To do this create a class that implements
116116
117- ``` php
117+ ``` php
118118Joomla\DI\ServiceProviderInterface
119119```
120120
121- . This gives you a register method which contains the container. You can
121+ This gives you a register method which contains the container. You can
122122then use the share method again to add any number of objects into the
123123container. You can then register this into the container with the
124- \ `\\ Joomla\\ DI\\ Container::registerServiceProvider\ ` method in the
124+ ` \\Joomla\\DI\\Container::registerServiceProvider ` method in the
125125container. You can see where we register all the core service providers
126126[ here in the \\ Joomla\\ CMS\\ Factory::createContainer
127127method] ( https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/Factory.php#L570-L594 )
128128
129+ :::caution TODO
130+
131+ Linking code directly to github for Joomla gets outdated really quickly since the branch we reference changes regulary.
132+ We need to find a better way to keep this links uptodate
133+
134+ :::
135+
129136## Component Containers
130137
131138Every component also has its own container (which is located in the
132139administrator section of Joomla). However this container is not exposed.
133140It's just there to get the system dependencies and allow a class to
134141represent your extension. This class is the Extension class and at a
135142minimum must implement the relevant extensions type interface. For
136- example a component must implement the
143+ example a component must implement the ` \Joomla\CMS\Extension\ComponentInterface `
144+ (found on [ GitHub] ( https://github.com/joomla/joomla-cms/blob/4.2-dev/libraries/src/Extension/ComponentInterface.php ) ).
137145
138- ``` php
139- \Joomla\CMS\Extension\ComponentInterface
140- ```
146+ :::caution TODO
147+
148+ Linking code directly to github for Joomla gets outdated really quickly since the branch we reference changes regulary.
149+ We need to find a better way to keep this links uptodate
150+
151+ :::
152+
153+ For full information on implementing this in your extension, we recommend reading
154+ [ Developing an MVC Component] ( https://docs.joomla.org/S:MyLanguage/J4.x:Developing_an_MVC_Component " wikilink ")
155+
156+
157+ :::caution TODO
158+
159+ Linking to docs.joomla.org should only be used for enduser documentation.
160+
161+ :::
141162
142- (found on [ here on
143- GitHub] ( https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/Extension/ComponentInterface.php ) ).
144- For full information on implementing this in your extension, we
145- recommend reading [ Developing an MVC
146- Component] ( https://docs.joomla.org/S:MyLanguage/J4.x:Developing_an_MVC_Component " wikilink ")
147163
148164### Using a component container in another extension
149165
150166You can easily grab the container of another extension through the
151- CMSApplication object. For
152- example
167+ CMSApplication object. For example
153168
154- ``` php
169+ ``` php
155170Factory::getApplication()->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Site');
156171```
157172
158- Will get the com \_ content container, get the MVC Factory and get the
173+ Will get the com_content container, get the MVC Factory and get the
159174ArticlesModel from the frontend of Joomla. And this will work in any
160175extension in frontend, backend or the API of Joomla (unlike the old
161- LegacyModel::getInstance() method)
176+ ` LegacyModel::getInstance() ` method)
162177
163178## Read More
164179
165180There's a great example in the Joomla Framework docs on why Dependency
166181Injection is good for your Application and how DIC's help structure it.
167- [ Read it
168- here] ( https://github.com/joomla-framework/di/blob/2.0-dev/docs/why-dependency-injection.md )
182+ [ Read it here] ( https://github.com/joomla-framework/di/blob/2.0-dev/docs/why-dependency-injection.md )
0 commit comments