Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
Create basic symfony bundle configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
k911 committed May 20, 2018
1 parent 472a834 commit b11a078
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 12 deletions.
53 changes: 53 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace App\Bundle\SwooleBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
private $builder;

public function __construct(TreeBuilder $builder)
{
$this->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());
}
}
51 changes: 51 additions & 0 deletions DependencyInjection/SwooleExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace App\Bundle\SwooleBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class SwooleExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container): void
{
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->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();
}
}
14 changes: 14 additions & 0 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -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]
27 changes: 15 additions & 12 deletions Server/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit b11a078

Please sign in to comment.