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

Merge branch 'add-config' into 'main'

Add a bundle configuration example

Closes #2

See merge request dbp/middleware/dbp-api/api-starter-bundle!19
parents 1e233d32 cc5c4582
No related branches found
No related tags found
1 merge request!19Add a bundle configuration example
Pipeline #105664 passed
......@@ -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`
......
......@@ -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
<?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;
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)
......
......@@ -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
......@@ -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');
......
<?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
$c->loadFromExtension('framework', [
'test' => true,
]);
$c->loadFromExtension('dbp_starter', [
'secret_token' => 'secret-test',
]);
}
}
......@@ -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);
}
}
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