diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 00000000..528d180c --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,53 @@ +builder = $builder; + } + + /** + * {@inheritdoc} + * + * @throws \InvalidArgumentException + * @throws \RuntimeException + */ + public function getConfigTreeBuilder(): TreeBuilder + { + $rootNode = $this->builder->root('swoole'); + + $rootNode + ->children() + ->arrayNode('server') + ->children() + ->scalarNode('host') + ->defaultValue('127.0.0.1') + ->end() + ->integerNode('port') + ->min(0) + ->max(65535) + ->defaultValue(9501) + ->end() + ->end() + ->end() // server + ->end() + ; + + return $this->builder; + } + + public static function fromTreeBuilder(): self + { + return new self(new TreeBuilder()); + } +} diff --git a/DependencyInjection/SwooleExtension.php b/DependencyInjection/SwooleExtension.php new file mode 100644 index 00000000..6625c973 --- /dev/null +++ b/DependencyInjection/SwooleExtension.php @@ -0,0 +1,51 @@ +load('services.yaml'); + + $configuration = Configuration::fromTreeBuilder(); + $config = $this->processConfiguration($configuration, $configs); + } + + /** + * {@inheritdoc} + */ + public function getAlias(): string + { + return 'swoole'; + } + + /** + * {@inheritdoc} + */ + public function getConfiguration(array $config, ContainerBuilder $container): Configuration + { + return Configuration::fromTreeBuilder(); + } +} diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml new file mode 100644 index 00000000..aa1db4a7 --- /dev/null +++ b/Resources/config/services.yaml @@ -0,0 +1,14 @@ +services: + + # default configuration for services in *this* file + _defaults: + # automatically injects dependencies in your services + autowire: true + # automatically registers your services as commands, event subscribers, etc. + autoconfigure: true + # this means you cannot fetch services directly from the container via $container->get() + # if you need to do this, you can override this setting on individual services + public: false + + App\Bundle\SwooleBundle\Server\Counter: + factory: [App\Bundle\SwooleBundle\Server\Counter, fromZero] \ No newline at end of file diff --git a/Server/Counter.php b/Server/Counter.php index f574a24b..70ce537e 100644 --- a/Server/Counter.php +++ b/Server/Counter.php @@ -4,31 +4,34 @@ namespace App\Bundle\SwooleBundle\Server; -use OutOfRangeException; +use Swoole\Atomic; final class Counter { - private $counter = 0; + private $counter; - /** - * @throws \OutOfRangeException - */ - public function increment(): void + private function __construct(Atomic $counter) { - if (PHP_INT_MAX === $this->counter) { - throw new OutOfRangeException('Exceeded maximum value for integer'); - } + $this->counter = $counter; + } - ++$this->counter; + public function increment(): void + { + $this->counter->add(1); } public function get(): int { - return $this->counter; + return $this->counter->get(); } public function reset(): void { - $this->counter = 0; + $this->counter->set(0); + } + + public static function fromZero(): self + { + return new self(new Atomic(0)); } }