|
| 1 | +# Modules and third-party libraries |
| 2 | + |
| 3 | +## Modules |
| 4 | + |
| 5 | +To enable a module you need to tell to `composer.json` file where to autoload module's namespace. Suppose that your |
| 6 | +new module will reside in `src/Mymodule` directory, and the module test at `test/MyModule`. |
| 7 | + |
| 8 | +```json |
| 9 | +{ |
| 10 | + ..., |
| 11 | + "autoload": { |
| 12 | + "psr-4": { |
| 13 | + "App\\": "src/App/", |
| 14 | + "MyModule\\": "src/MyModule/" |
| 15 | + } |
| 16 | + }, |
| 17 | + "autoload-dev": { |
| 18 | + "psr-4": { |
| 19 | + "AppTest\\": "test/App/", |
| 20 | + "MyModuleTest\\": "test/MyModule/" |
| 21 | + } |
| 22 | + }, |
| 23 | + ... |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +# Third Party libraries |
| 28 | + |
| 29 | +Antidot framework is an open-source ecosystem friendly, and it allows easily to manage third-party libraries or packages |
| 30 | +making use of the [Laminas Config Provider](https://docs.laminas.dev/laminas-config-aggregator/config-providers/) component. |
| 31 | + |
| 32 | + With Composer, you can install any package available in [Packagist](https://packagist.org/), then if you want to have it available in the DI container, |
| 33 | + you need to configure wanted dependency in a config file following [the config instructions](/framework/dependency-injection.md). |
| 34 | + |
| 35 | + When you want to add complex objects to the DI container you can take advantage of using factory classes, these classes |
| 36 | + must be invokables with a PSR-11 container interface as first parameter, and as recommendation, |
| 37 | + it doesn't have to have any dependency in its constructor. |
| 38 | + |
| 39 | +For example integrating [Doctrine DBAL](https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/index.html) connection for MySQL: |
| 40 | + |
| 41 | +Install it by composer |
| 42 | + |
| 43 | +```bash |
| 44 | +composer require doctrine/dbal |
| 45 | +``` |
| 46 | + |
| 47 | +Create a factory class for example in `App\Container` namespace. |
| 48 | + |
| 49 | +```php |
| 50 | +<?php |
| 51 | +# src/Container/DbalConnectionFactory |
| 52 | + |
| 53 | +declare(strict_types=1); |
| 54 | + |
| 55 | +namespace App\Container; |
| 56 | + |
| 57 | +use Doctrine\DBAL\Configuration; |
| 58 | +use Doctrine\DBAL\Connection; |
| 59 | +use Doctrine\DBAL\DriverManager; |
| 60 | +use Psr\Container\ContainerInterface; |
| 61 | + |
| 62 | +class DoctrineDBALFactory |
| 63 | +{ |
| 64 | + public function __invoke(ContainerInterface $container): Connection |
| 65 | + { |
| 66 | + $config = $container->get('config')['dbal']['connection']; |
| 67 | + |
| 68 | + return DriverManager::getConnection($config, new Configuration()); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Register factory in the [Dependency Injection Container](/framework/dependency-injection.md) and add config parameter to it. |
| 74 | + |
| 75 | +```yaml |
| 76 | +# See dependency injection container config docs to more info about service |
| 77 | +# configuration https://antidotfw.io/framework/dependency-injection.md. |
| 78 | +parameters: |
| 79 | + dbal: |
| 80 | + connection: |
| 81 | + dbname: db_name |
| 82 | + user: db_user |
| 83 | + password: db_secret |
| 84 | + host: localhost |
| 85 | + port: 3306 |
| 86 | + driver: pdo_mysql |
| 87 | + options: |
| 88 | + # this enables PDO to throw exceptions |
| 89 | + 3: 2 |
| 90 | + |
| 91 | +services: |
| 92 | + Doctrine\DBAL\Connection: |
| 93 | + factory: App\Container\DoctrineDBALFactory |
| 94 | +``` |
0 commit comments