Skip to content

Commit

Permalink
Config files update
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Feb 27, 2024
1 parent 7a3e9ea commit 09438e1
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 202 deletions.
60 changes: 0 additions & 60 deletions src/DependencyInjection/Configuration.php

This file was deleted.

126 changes: 0 additions & 126 deletions src/DependencyInjection/TelegramBotExtension.php

This file was deleted.

24 changes: 16 additions & 8 deletions src/TelegramBotBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@

namespace Luzrain\TelegramBotBundle;

use Luzrain\TelegramBotBundle\DependencyInjection\CommandCompilerPass;
use Luzrain\TelegramBotBundle\DependencyInjection\TelegramBotExtension;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

final class TelegramBotBundle extends Bundle
final class TelegramBotBundle extends AbstractBundle
{
protected string $extensionAlias = 'telegram_bot';

public function configure(DefinitionConfigurator $definition): void
{
$configurator = require __DIR__ . '/config/configuration.php';
$configurator($definition);
}

public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new CommandCompilerPass());
$container->addCompilerPass(require __DIR__ . '/config/compilerpass.php');
}

public function getContainerExtension(): ExtensionInterface
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
return new TelegramBotExtension();
$configurator = require __DIR__ . '/config/services.php';
$configurator($config, $builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\DependencyInjection;

use Luzrain\TelegramBotBundle\TelegramBot\CommandMetadataProvider;
use Luzrain\TelegramBotBundle\TelegramBot\UpdateHandler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;

final class CommandCompilerPass implements CompilerPassInterface
{
/**
* @psalm-suppress ArgumentTypeCoercion
*/
return new class () implements CompilerPassInterface {
public function process(ContainerBuilder $container): void
{
$controllers = $container->findTaggedServiceIds('telegram_bot.command');
Expand Down Expand Up @@ -69,4 +63,4 @@ private function referenceMap(array $serviceClasses): array
}
return $result;
}
}
};
50 changes: 50 additions & 0 deletions src/config/configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Luzrain\TelegramBotBundle\config;

use Luzrain\TelegramBotApi\Type\Update;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;

/** @php-cs-fixer-ignore */
return static function (DefinitionConfigurator $definition) {
$definition->rootNode()
->children()
->scalarNode('http_client')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('request_factory')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('stream_factory')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('api_token')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('secret_token')
->defaultNull()
->end()
->arrayNode('allowed_updates')
->prototype('scalar')->end()
->beforeNormalization()
->always(fn ($values) => array_map(strval(...), $values))
->end()
->validate()
->ifTrue(fn ($configArray) => array_diff($configArray, Update::getUpdateTypes()) !== [])
->then(function ($configArray) {
if (array_diff($configArray, Update::getUpdateTypes()) !== []) {
$allowedKeys = implode(', ', Update::getUpdateTypes());
throw new \InvalidArgumentException(sprintf('Invalid updates list. Allowed updates: %s', $allowedKeys));
}
return $configArray;
})
->end()
->end()
->end();
};
Loading

0 comments on commit 09438e1

Please sign in to comment.