Skip to content
Snippets Groups Projects
Commit cc5c4582 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Add a bundle configuration example

This adds one config entry and injects the value into a service when the bundle gets loaded.

See the README changes for how to set the config in the symfony app.

Fixes #2
parent 1e233d32
No related branches found
No related tags found
1 merge request!19Add a bundle configuration example
Pipeline #105663 passed
...@@ -47,6 +47,26 @@ DBP\API\CoreBundle\DbpCoreBundle::class => ['all' => true], ...@@ -47,6 +47,26 @@ DBP\API\CoreBundle\DbpCoreBundle::class => ['all' => true],
* Run `composer install` to clear caches * 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 ## Development & Testing
* Install dependencies: `composer install` * Install dependencies: `composer install`
......
...@@ -10,4 +10,7 @@ parameters: ...@@ -10,4 +10,7 @@ parameters:
bootstrapFiles: bootstrapFiles:
- vendor/bin/.phpunit/phpunit-9-0/vendor/autoload.php - vendor/bin/.phpunit/phpunit-9-0/vendor/autoload.php
excludes_analyse: excludes_analyse:
- tests/bootstrap.php - tests/bootstrap.php
\ No newline at end of file ignoreErrors:
- message: '#.*NodeDefinition::children.*#'
path: ./src/DependencyInjection
<?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;
}
}
...@@ -7,11 +7,11 @@ namespace DBP\API\StarterBundle\DependencyInjection; ...@@ -7,11 +7,11 @@ namespace DBP\API\StarterBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; 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( $this->extendArrayParameter(
$container, 'api_platform.resource_class_directories', [__DIR__.'/../Entity']); $container, 'api_platform.resource_class_directories', [__DIR__.'/../Entity']);
...@@ -21,6 +21,10 @@ class DbpStarterExtension extends Extension ...@@ -21,6 +21,10 @@ class DbpStarterExtension extends Extension
new FileLocator(__DIR__.'/../Resources/config') new FileLocator(__DIR__.'/../Resources/config')
); );
$loader->load('services.yaml'); $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) private function extendArrayParameter(ContainerBuilder $container, string $parameter, array $values)
......
...@@ -20,5 +20,9 @@ services: ...@@ -20,5 +20,9 @@ services:
autowire: true autowire: true
autoconfigure: true autoconfigure: true
DBP\API\StarterBundle\Service\MyCustomService:
autowire: true
autoconfigure: true
DBP\API\StarterBundle\Service\PlaceProviderInterface: DBP\API\StarterBundle\Service\PlaceProviderInterface:
'@DBP\API\StarterBundle\Service\ExternalApi' '@DBP\API\StarterBundle\Service\ExternalApi'
\ No newline at end of file
...@@ -10,8 +10,10 @@ class ExternalApi implements PlaceProviderInterface ...@@ -10,8 +10,10 @@ class ExternalApi implements PlaceProviderInterface
{ {
private $places; private $places;
public function __construct() public function __construct(MyCustomService $service)
{ {
$service = $service;
$this->places = []; $this->places = [];
$place1 = new Place(); $place1 = new Place();
$place1->setIdentifier('graz'); $place1->setIdentifier('graz');
......
<?php
declare(strict_types=1);
namespace DBP\API\StarterBundle\Service;
class MyCustomService
{
private $token;
public function __construct(string $token)
{
$this->token = $token;
}
}
...@@ -42,5 +42,9 @@ class Kernel extends BaseKernel ...@@ -42,5 +42,9 @@ class Kernel extends BaseKernel
$c->loadFromExtension('framework', [ $c->loadFromExtension('framework', [
'test' => true, 'test' => true,
]); ]);
$c->loadFromExtension('dbp_starter', [
'secret_token' => 'secret-test',
]);
} }
} }
...@@ -5,6 +5,7 @@ declare(strict_types=1); ...@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace DBP\API\StarterBundle\Tests\Service; namespace DBP\API\StarterBundle\Tests\Service;
use DBP\API\StarterBundle\Service\ExternalApi; use DBP\API\StarterBundle\Service\ExternalApi;
use DBP\API\StarterBundle\Service\MyCustomService;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class ExternalApiTest extends WebTestCase class ExternalApiTest extends WebTestCase
...@@ -13,11 +14,13 @@ class ExternalApiTest extends WebTestCase ...@@ -13,11 +14,13 @@ class ExternalApiTest extends WebTestCase
protected function setUp(): void protected function setUp(): void
{ {
$this->api = new ExternalApi(); $service = new MyCustomService('secret-test-custom');
$this->api = new ExternalApi($service);
} }
public function test() public function test()
{ {
$this->assertTrue(true); $this->assertTrue(true);
$this->assertNotNull($this->api);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment