Skip to content

Commit bc8b87c

Browse files
authored
Merge pull request #7737 from cakephp/plugin-loading
Update docs related to plugin loading.
2 parents 95b6c74 + 348c6ac commit bc8b87c

File tree

2 files changed

+69
-94
lines changed

2 files changed

+69
-94
lines changed

en/appendices/5-0-migration-guide.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ Core
296296
----
297297

298298
- The ``services()`` method was added to ``PluginInterface``.
299-
- ``PluginCollection::addFromConfig()`` has been added to :ref:`simplify plugin loading <loading-plugins-via-configuration-array>`.
299+
- ``PluginCollection::addFromConfig()`` has been added to :ref:`simplify plugin loading <loading-a-plugin>`.
300300

301301
Database
302302
--------
@@ -400,3 +400,9 @@ TestSuite
400400
---------
401401

402402
- ``IntegrationTestTrait::requestAsJson()`` has been added to set JSON headers for the next request.
403+
404+
Plugin Installer
405+
----------------
406+
- The plugin installer has been updated to automatically handle class autoloading
407+
for your app plugins. So you can remove the namespace to path mappings for your
408+
plugins from your ``composer.json`` and just run ``composer dumpautoload``.

en/plugins.rst

Lines changed: 62 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -51,83 +51,52 @@ Manually Autoloading Plugin Classes
5151
If you install your plugins via ``composer`` or ``bake`` you shouldn't need to
5252
configure class autoloading for your plugins.
5353

54-
If we were installing a plugin named ``MyPlugin`` manually you would need to
55-
modify your application's **composer.json** file to contain the following
56-
information:
54+
If you create a plugin manually under the ``plugins`` folder then will need to
55+
tell ``composer`` to refresh its autoloading cache:
5756

58-
.. code-block:: json
57+
.. code-block:: console
5958
60-
{
61-
"autoload": {
62-
"psr-4": {
63-
"MyPlugin\\": "plugins/MyPlugin/src/"
64-
}
65-
},
66-
"autoload-dev": {
67-
"psr-4": {
68-
"MyPlugin\\Test\\": "plugins/MyPlugin/tests/"
69-
}
70-
}
71-
}
59+
php composer.phar dumpautoload
7260
73-
If you are using vendor namespaces for your plugins, the namespace to path mapping
74-
should resemble the following:
61+
If you are using vendor namespaces for your plugins, you'll have to add the
62+
namespace to path mapping to the ``composer.json`` resembling the following
63+
before running the above composer command:
7564

7665
.. code-block:: json
7766
7867
{
7968
"autoload": {
8069
"psr-4": {
8170
"AcmeCorp\\Users\\": "plugins/AcmeCorp/Users/src/",
71+
}
72+
},
73+
"autoload-dev": {
74+
"psr-4": {
8275
"AcmeCorp\\Users\\Test\\": "plugins/AcmeCorp/Users/tests/"
8376
}
8477
}
8578
}
8679
87-
Additionally, you will need to tell Composer to refresh its autoloading cache:
88-
89-
.. code-block:: console
90-
91-
php composer.phar dumpautoload
80+
.. _loading-a-plugin:
9281

9382
Loading a Plugin
9483
================
9584

9685
If you want to use a plugin's routes, console commands, middlewares, event
9786
listeners, templates or webroot assets you will need to load the plugin.
98-
Plugins are loaded in your application's ``bootstrap()`` function::
99-
100-
// In src/Application.php
101-
use Cake\Http\BaseApplication;
102-
use ContactManager\ContactManagerPlugin;
103-
104-
class Application extends BaseApplication {
105-
public function bootstrap()
106-
{
107-
parent::bootstrap();
108-
// Load the contact manager plugin by class name
109-
$this->addPlugin(ContactManagerPlugin::class);
110-
111-
// Load a plugin with a vendor namespace by 'short name'
112-
$this->addPlugin('AcmeCorp/ContactManager');
113-
114-
// Load a dev dependency that will not exist in production builds.
115-
$this->addOptionalPlugin('AcmeCorp/ContactManager');
116-
}
117-
}
11887

11988
If you just want to use helpers, behaviors or components from a plugin you do
12089
not need to explicitly load a plugin yet it's recommended to always do so.
12190

122-
There is also a handy console command to enable the plugin. Execute the following
91+
There is also a handy console command to load the plugin. Execute the following
12392
line:
12493

12594
.. code-block:: console
12695
12796
bin/cake plugin load ContactManager
12897
129-
This would update your application's bootstrap method, or put the
130-
``$this->addPlugin('ContactManager');`` snippet in the bootstrap for you.
98+
This would update the array in your application's ``config/plugins.php`` with
99+
an entry similar to ``'ContactManager' => []``.
131100

132101
.. _plugin-configuration:
133102

@@ -147,16 +116,52 @@ appropriate parts of your application. The hooks are:
147116
collection.
148117
* ``services`` Used to register application container services
149118

150-
When loading plugins you can configure which hooks are enabled. By default
151-
plugins without a :ref:`plugin-objects` have all hooks disabled. New style plugins
152-
allow plugin authors to set defaults, which can be configured by you in your
153-
application::
119+
By default all plugins hooks are enabled. You can disable hooks by using the
120+
related options of the ``plugin load`` command::
154121

155-
// In Application::bootstrap()
122+
.. code-block:: console
123+
124+
bin/cake plugin load ContactManager --no-routes
125+
126+
This would update the array in your application's ``config/plugins.php`` with
127+
an entry similar to ``'ContactManager' => ['routes' => false]``.
128+
129+
Plugin Loading Options
130+
======================
131+
132+
Apart from the options for plugin hooks the ``plugin load`` command has the
133+
following options to control plugin loading:
134+
135+
- ``--only-debug`` Load the plugin only when debug mode is enabled.
136+
- ``--only-cli`` Load the plugin only for CLI.
137+
- ``--optional`` Do not throw an error if the plugin is not available.
138+
139+
Loading plugins through ``Application::bootstrap()``
140+
====================================================
141+
142+
Apart from the config array in ``config/plugins.php``, plugins can also be
143+
loaded in your application's ``bootstrap()`` method::
144+
145+
// In src/Application.php
146+
use Cake\Http\BaseApplication;
156147
use ContactManager\ContactManagerPlugin;
157148

158-
// Disable routes for the ContactManager plugin
159-
$this->addPlugin(ContactManagerPlugin::class, ['routes' => false]);
149+
class Application extends BaseApplication
150+
{
151+
public function bootstrap()
152+
{
153+
parent::bootstrap();
154+
155+
// Load the contact manager plugin by class name
156+
$this->addPlugin(ContactManagerPlugin::class);
157+
158+
// Load a plugin with a vendor namespace by 'short name' with options
159+
$this->addPlugin('AcmeCorp/ContactManager', ['console' => false]);
160+
161+
// Load a dev dependency that will not exist in production builds.
162+
$this->addOptionalPlugin('AcmeCorp/ContactManager');
163+
}
164+
}
160165

161166
You can configure hooks with array options, or the methods provided by plugin
162167
classes::
@@ -183,33 +188,6 @@ Plugin objects also know their names and path information::
183188
$path = $plugin->getConfigPath();
184189
$path = $plugin->getClassPath();
185190

186-
.. _loading-plugins-via-configuration-array:
187-
188-
Loading plugins via configuration array
189-
=======================================
190-
191-
.. versionadded:: 5.0.0
192-
193-
Alternatively you can also load plugins via adding a ``config/plugins.php``
194-
to your application with the following content::
195-
196-
<?php
197-
return [
198-
'Company/TestPluginThree',
199-
'TestPlugin' => ['onlyDebug' => true],
200-
'Named' => ['routes' => false],
201-
];
202-
203-
You still have full control over which plugins are loaded in debug and/or
204-
CLI mode only via the following config values:
205-
206-
- ``onlyDebug``
207-
- ``onlyCli``
208-
- ``optional``
209-
210-
The usual plugin hooks ``bootstrap``, ``routes``, ``middleware`` and ``console``
211-
are available as well.
212-
213191
Using Plugin Classes
214192
====================
215193

@@ -274,7 +252,8 @@ Note the name of the plugin folder, '**ContactManager**'. It is important
274252
that this folder has the same name as the plugin.
275253

276254
Inside the plugin folder, you'll notice it looks a lot like a CakePHP
277-
application, and that's basically what it is. You don't have to
255+
application, and that's basically what it is. Just instead of an ``Application.php``
256+
you have a ``ContactManagerPlugin.php``. You don't have to
278257
include any of the folders you are not using. Some plugins might
279258
only define a Component and a Behavior, and in that case they can completely
280259
omit the 'templates' directory.
@@ -316,7 +295,7 @@ Plugin Objects
316295

317296
Plugin Objects allow a plugin author to define set-up logic, define default
318297
hooks, load routes, middleware and console commands. Plugin objects live in
319-
**src/Plugin.php**. For our ContactManager plugin, our plugin class could look
298+
**src/<PluginName>Plugin.php**. For our ContactManager plugin, our plugin class could look
320299
like::
321300

322301
namespace ContactManager;
@@ -378,7 +357,7 @@ like::
378357
*
379358
* @param \Cake\Core\ContainerInterface $container The Container to update.
380359
* @return void
381-
* @link https://book.cakephp.org/4/en/development/dependency-injection.html#dependency-injection
360+
* @link https://book.cakephp.org/5/en/development/dependency-injection.html#dependency-injection
382361
*/
383362
public function services(ContainerInterface $container): void
384363
{
@@ -415,12 +394,6 @@ ContactManager plugin routes, put the following into
415394
The above will connect default routes for your plugin. You can customize this
416395
file with more specific routes later on.
417396

418-
Before you can access your controllers, you'll need to ensure the plugin is
419-
loaded and the plugin routes are loaded. In your **src/Application.php** add
420-
the following::
421-
422-
$this->addPlugin('ContactManager', ['routes' => true]);
423-
424397
You can also load plugin routes in your application's routes list. Doing this
425398
provides you more control on how plugin routes are loaded and allows you to wrap
426399
plugin routes in additional scopes or prefixes::
@@ -494,10 +467,6 @@ also connect routes that use the following pattern::
494467
See the section on :ref:`plugin-configuration` for information on how to load
495468
plugin specific route files.
496469

497-
For plugins you did not create with bake, you will also need to edit the
498-
**composer.json** file to add your plugin to the autoload classes, this can be
499-
done as per the documentation :ref:`autoloading-plugin-classes`.
500-
501470
.. _plugin-models:
502471

503472
Plugin Models

0 commit comments

Comments
 (0)