From b51086c8d4aee3bfa1c1eeab03136bd7b19c0175 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Thu, 22 Apr 2021 12:25:50 +0200
Subject: [PATCH] Add a bundle configuration example

Fixes #2
---
 README.md                                     | 20 ++++++++++++++++
 phpstan.neon                                  |  5 +++-
 src/DependencyInjection/Configuration.php     | 24 +++++++++++++++++++
 .../DbpStarterExtension.php                   | 10 +++++---
 src/Resources/config/services.yaml            |  4 ++++
 src/Service/ExternalApi.php                   |  4 +++-
 src/Service/MyCustomService.php               | 16 +++++++++++++
 tests/Kernel.php                              |  4 ++++
 tests/Service/ExternalApiTest.php             |  5 +++-
 9 files changed, 86 insertions(+), 6 deletions(-)
 create mode 100644 src/DependencyInjection/Configuration.php
 create mode 100644 src/Service/MyCustomService.php

diff --git a/README.md b/README.md
index 3949663..c0d1153 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 a0f2301..6cde1f0 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 0000000..b1ac473
--- /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 7e03ac7..8cfbf94 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 9916eaf..c326a10 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 0bd675d..6f5dd73 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 0000000..5056d7e
--- /dev/null
+++ b/src/Service/MyCustomService.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DBP\API\StarterBundle\Service;
+
+class MyCustomService
+{
+    private $token;
+
+    public function __construct(string $token)
+    {
+        $this->token = $token;
+        dump($token);
+    }
+}
diff --git a/tests/Kernel.php b/tests/Kernel.php
index 4c9936e..a71d093 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 4784220..c37060e 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);
     }
 }
-- 
GitLab