|
| 1 | +# How can I make my application modular? |
| 2 | + |
| 3 | +Zend Framework 2 applications have a concept of modules, independent units that |
| 4 | +can provide configuration, services, and hooks into its MVC lifecycle. This |
| 5 | +functionality is provided by zend-modulemanager. |
| 6 | + |
| 7 | +While zend-modulemanager could be used with Expressive, we suggest another |
| 8 | +approach: modules that are based only on configuration. This powerful approach |
| 9 | +doesn't affect performance, and offers extensive flexibility: each module can |
| 10 | +provide its own services (with factories), default configuration, and routes. |
| 11 | + |
| 12 | +This cookbook will show how to organize modules using |
| 13 | +[mtymek/expressive-config-manager](https://github.com/mtymek/expressive-config-manager), |
| 14 | +a lightweight library that aggregates and merges configuration, optionally caching it. |
| 15 | + |
| 16 | +## Install the configuration manager |
| 17 | + |
| 18 | +The configuration manager is available in Packagist: |
| 19 | + |
| 20 | +```bash |
| 21 | +$ composer require mtymek/expressive-config-manager |
| 22 | +``` |
| 23 | + |
| 24 | +## Generate your config |
| 25 | + |
| 26 | +The default Expressive skeleton installs a `config/config.php` file, which |
| 27 | +aggregates all configuration. When using the configuration manager, you will |
| 28 | +need to replace the contents of that file with the following code: |
| 29 | + |
| 30 | +```php |
| 31 | +<?php |
| 32 | + |
| 33 | +use Zend\Expressive\ConfigManager\ConfigManager; |
| 34 | +use Zend\Expressive\ConfigManager\PhpFileProvider; |
| 35 | + |
| 36 | +$configManager = new ConfigManager([ |
| 37 | + new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'), |
| 38 | +]); |
| 39 | + |
| 40 | +return new ArrayObject($configManager->getMergedConfig()); |
| 41 | +``` |
| 42 | + |
| 43 | +If you open your application in a browser, it should still work in exactly the |
| 44 | +same way as it was before. Now you can start adding your modules. |
| 45 | + |
| 46 | +## First module |
| 47 | + |
| 48 | +`ConfigManager` does not force you to use any particular structure for your |
| 49 | +module; its only requirement is to expose default configuration using a "config |
| 50 | +provider", which is simply an invokable class that returns a configuration |
| 51 | +array. |
| 52 | + |
| 53 | +For instance, this is how your module could provide its own routes: |
| 54 | + |
| 55 | +```php |
| 56 | +namespace App; |
| 57 | + |
| 58 | +class AppConfig |
| 59 | +{ |
| 60 | + public function __invoke() |
| 61 | + { |
| 62 | + return [ |
| 63 | + 'routes' => [ |
| 64 | + [ |
| 65 | + 'name' => 'api.list-transactions', |
| 66 | + 'path' => '/api/transactions', |
| 67 | + 'middleware' => App\Action\ListTransactionsAction::class, |
| 68 | + 'allowed_methods' => ['GET'], |
| 69 | + ], |
| 70 | + [ |
| 71 | + 'name' => 'api.refund-transaction', |
| 72 | + 'path' => '/api/refund', |
| 73 | + 'middleware' => App\Action\RefundAction::class, |
| 74 | + 'allowed_methods' => ['POST'], |
| 75 | + ], |
| 76 | + ], |
| 77 | + ]; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Enabling the module |
| 83 | + |
| 84 | +Finally, you can enable your module by adding a reference to your config class |
| 85 | +within the arguments of the `ConfigManager` constructor in the `config/config.php` |
| 86 | +file: |
| 87 | + |
| 88 | +```php |
| 89 | +$configManager = new ConfigManager([ |
| 90 | + App\AppConfig::class, |
| 91 | + new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'), |
| 92 | +]); |
| 93 | +``` |
| 94 | + |
| 95 | +## Caching configuration |
| 96 | + |
| 97 | +In order to provide configuration caching, two things must occur: |
| 98 | + |
| 99 | +- First, you must define a `config_cache_enabled` key in your configuration |
| 100 | + somewhere. |
| 101 | +- Second, you must pass a second argument to the `ConfigManager`, the location |
| 102 | + of the cache file to use. |
| 103 | + |
| 104 | +The `config_cache_enabled` key can be defined in any of your configuration |
| 105 | +providers, including the autoloaded configuration files. We recommend defining |
| 106 | +them in two locations: |
| 107 | + |
| 108 | +- `config/autoload/global.php` should define the value to `true`, as the |
| 109 | + production setting. |
| 110 | +- `config/autoload/local.php` should also define the setting, and use a value |
| 111 | + appropriate to the current environment. In development, for instance, this |
| 112 | + would be `false`. |
| 113 | + |
| 114 | +```php |
| 115 | +// config/autoload/global.php |
| 116 | + |
| 117 | +return [ |
| 118 | + 'config_cache_enabled' => true, |
| 119 | + /* ... */ |
| 120 | +]; |
| 121 | + |
| 122 | +// config/autoload/local.php |
| 123 | + |
| 124 | +return [ |
| 125 | + 'config_cache_enabled' => false, // <- development! |
| 126 | + /* ... */ |
| 127 | +]; |
| 128 | +``` |
| 129 | + |
| 130 | +You would then alter your `config/config.php` file to add the second argument. |
| 131 | +The following example builds on the previous, and demonstrates having the |
| 132 | +`AppConfig` entry enabled. The configuration will be cached to |
| 133 | +`data/config-cache.php` in the application root: |
| 134 | + |
| 135 | +```php |
| 136 | +$configManager = new ConfigManager([ |
| 137 | + App\AppConfig::class, |
| 138 | + new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'), |
| 139 | +], 'data/config-cache.php'); |
| 140 | +``` |
| 141 | + |
| 142 | +When the configuration cache path is present, if the `config_cache_enabled` flag |
| 143 | +is enabled, then configuration will be read from the cached configuration, |
| 144 | +instead of parsing and merging the various configuration sources. |
| 145 | + |
| 146 | +## Final notes |
| 147 | + |
| 148 | +This approach may look simple, but it is flexible and powerful: |
| 149 | + |
| 150 | +- You pass a list of config providers to the `ConfigManager` constructor. |
| 151 | +- Configuration is merged in the same order as it is passed, with later entries |
| 152 | + having precedence. |
| 153 | +- You can override module configuration using `*.global.php` and `*.local.php` files. |
| 154 | +- If cached config is found, `ConfigManager` does not iterate over provider list. |
| 155 | + |
| 156 | +For more details, please refer to the |
| 157 | +[Config Manager Documentation](https://github.com/mtymek/expressive-config-manager#expressive-configuration-manager). |
0 commit comments