diff --git a/README.md b/README.md index 3949663babbd8809f85e7d5d033fd01fdc52b596..c0d115318aa8ec59c0016af674f729670105355a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,26 @@ DBP\API\CoreBundle\DbpCoreBundle::class => ['all' => true], * Run `composer install` to clear caches +## Configuration + +The bundle has a `secret_token` configuration value that you can specify in your +app, either by hardcoding it, or by referencing an environment variable. + +For this create `config/packages/dbp_starter.yaml` in the app with the following +content: + +```yaml +dbp_starter: + secret_token: 42 + # secret_token: '%env(SECRET_TOKEN)%' +``` + +The value gets read in `DbpStarterExtension` and passed when creating the +`MyCustomService` service. + +For more info on bundle configuration see +https://symfony.com/doc/current/bundles/configuration.html + ## Development & Testing * Install dependencies: `composer install` diff --git a/phpstan.neon b/phpstan.neon index a0f2301d23b15d2b2354aafc7c82215fabd8d740..6cde1f0f4471f841c1c559ba28c81d8fad5986e6 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,4 +10,7 @@ parameters: bootstrapFiles: - vendor/bin/.phpunit/phpunit-9-0/vendor/autoload.php excludes_analyse: - - tests/bootstrap.php \ No newline at end of file + - tests/bootstrap.php + ignoreErrors: + - message: '#.*NodeDefinition::children.*#' + path: ./src/DependencyInjection diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php new file mode 100644 index 0000000000000000000000000000000000000000..b1ac473f1fc2c2565e52edf43e28e2bac0d3ecd5 --- /dev/null +++ b/src/DependencyInjection/Configuration.php @@ -0,0 +1,24 @@ +<?php + +declare(strict_types=1); + +namespace DBP\API\StarterBundle\DependencyInjection; + +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\ConfigurationInterface; + +class Configuration implements ConfigurationInterface +{ + public function getConfigTreeBuilder() + { + $treeBuilder = new TreeBuilder('dbp_starter'); + + $treeBuilder->getRootNode() + ->children() + ->scalarNode('secret_token')->end() + ->end() + ->end(); + + return $treeBuilder; + } +} diff --git a/src/DependencyInjection/DbpStarterExtension.php b/src/DependencyInjection/DbpStarterExtension.php index 7e03ac7b1f5d8901ab05da6f4b4ded7292c4fdd8..8cfbf947f53ce30d41680e7aac21947d3520caa4 100644 --- a/src/DependencyInjection/DbpStarterExtension.php +++ b/src/DependencyInjection/DbpStarterExtension.php @@ -7,11 +7,11 @@ namespace DBP\API\StarterBundle\DependencyInjection; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; -class DbpStarterExtension extends Extension +class DbpStarterExtension extends ConfigurableExtension { - public function load(array $configs, ContainerBuilder $container) + public function loadInternal(array $mergedConfig, ContainerBuilder $container) { $this->extendArrayParameter( $container, 'api_platform.resource_class_directories', [__DIR__.'/../Entity']); @@ -21,6 +21,10 @@ class DbpStarterExtension extends Extension new FileLocator(__DIR__.'/../Resources/config') ); $loader->load('services.yaml'); + + // Inject the config value into the MyCustomService service + $definition = $container->getDefinition('DBP\API\StarterBundle\Service\MyCustomService'); + $definition->addArgument($mergedConfig['secret_token']); } private function extendArrayParameter(ContainerBuilder $container, string $parameter, array $values) diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 9916eaff27edc9a20b07309c6599c780425b358f..c326a10165c631bc8eef25cf472634c84afa2ac7 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -20,5 +20,9 @@ services: autowire: true autoconfigure: true + DBP\API\StarterBundle\Service\MyCustomService: + autowire: true + autoconfigure: true + DBP\API\StarterBundle\Service\PlaceProviderInterface: '@DBP\API\StarterBundle\Service\ExternalApi' \ No newline at end of file diff --git a/src/Service/ExternalApi.php b/src/Service/ExternalApi.php index 0bd675d1b1681cab19c006177125f3173dd434bf..6f5dd7331aeb609134479efdaa2e7334f951554d 100644 --- a/src/Service/ExternalApi.php +++ b/src/Service/ExternalApi.php @@ -10,8 +10,10 @@ class ExternalApi implements PlaceProviderInterface { private $places; - public function __construct() + public function __construct(MyCustomService $service) { + $service = $service; + $this->places = []; $place1 = new Place(); $place1->setIdentifier('graz'); diff --git a/src/Service/MyCustomService.php b/src/Service/MyCustomService.php new file mode 100644 index 0000000000000000000000000000000000000000..23c60e98ae62b3d23f26f3ffbfadcbdcf61f86c3 --- /dev/null +++ b/src/Service/MyCustomService.php @@ -0,0 +1,15 @@ +<?php + +declare(strict_types=1); + +namespace DBP\API\StarterBundle\Service; + +class MyCustomService +{ + private $token; + + public function __construct(string $token) + { + $this->token = $token; + } +} diff --git a/tests/Kernel.php b/tests/Kernel.php index 4c9936ea5b8fe1033872d2156772c43dfce4d416..a71d0937b276af6c4d22a38ab7ab296a70dce67b 100644 --- a/tests/Kernel.php +++ b/tests/Kernel.php @@ -42,5 +42,9 @@ class Kernel extends BaseKernel $c->loadFromExtension('framework', [ 'test' => true, ]); + + $c->loadFromExtension('dbp_starter', [ + 'secret_token' => 'secret-test', + ]); } } diff --git a/tests/Service/ExternalApiTest.php b/tests/Service/ExternalApiTest.php index 4784220da427a3b255103e9afa18147d747e05ff..c37060e41ff96b66e21fcdd58181f7b1b866df9e 100644 --- a/tests/Service/ExternalApiTest.php +++ b/tests/Service/ExternalApiTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace DBP\API\StarterBundle\Tests\Service; use DBP\API\StarterBundle\Service\ExternalApi; +use DBP\API\StarterBundle\Service\MyCustomService; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class ExternalApiTest extends WebTestCase @@ -13,11 +14,13 @@ class ExternalApiTest extends WebTestCase protected function setUp(): void { - $this->api = new ExternalApi(); + $service = new MyCustomService('secret-test-custom'); + $this->api = new ExternalApi($service); } public function test() { $this->assertTrue(true); + $this->assertNotNull($this->api); } }