Skip to content

Commit

Permalink
Move common code from phpbenchmarks/symlex to phpbenchmarks/symlex-c…
Browse files Browse the repository at this point in the history
…ommon
  • Loading branch information
steevanb committed Dec 18, 2018
1 parent 26d095a commit a537c05
Show file tree
Hide file tree
Showing 9 changed files with 15,236 additions and 1 deletion.
46 changes: 46 additions & 0 deletions services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
services:
benchmark.normalizer.user:
class: PhpBenchmarksSymlex\Normalizer\UserNormalizer
public: false
arguments: ['@translator']

benchmark.normalizer.comment:
class: PhpBenchmarksSymlex\Normalizer\CommentNormalizer
public: false
arguments: ['@translator']

benchmark.normalizer.comment_type:
class: PhpBenchmarksSymlex\Normalizer\CommentTypeNormalizer
public: false
arguments: ['@translator']

serializer:
class: Symfony\Component\Serializer\Serializer
arguments: [ [ "@benchmark.normalizer.user", "@benchmark.normalizer.comment", "@benchmark.normalizer.comment_type" ], [] ]

benchmark.event_listener.define_locale:
class: PhpBenchmarksSymlex\EventListener\DefineLocaleEventListener
arguments: ['@translator']

event_dispatcher:
class: Symfony\Component\EventDispatcher\EventDispatcher
calls:
- [ addListener, ['defineLocale', ["@benchmark.event_listener.define_locale", 'onDefineLocale']]]

yaml_loader:
class: Symfony\Component\Translation\Loader\YamlFileLoader

translator:
class: Symfony\Component\Translation\Translator
arguments: [ "en" ]
calls:
- [ setFallbackLocales, [['en']] ]
- [ addLoader, [ "yaml", "@yaml_loader" ] ]
- [ addResource, [ "yaml", "%app.path%/../vendor/phpbenchmarks/symlex-common/src/L10n/phpbenchmarks.en.yml", "en", "phpbenchmarks" ] ]
- [ addResource, [ "yaml", "%app.path%/../vendor/phpbenchmarks/symlex-common/src/L10n/phpbenchmarks.en_GB.yml", "en_GB", "phpbenchmarks" ] ]
- [ addResource, [ "yaml", "%app.path%/../vendor/phpbenchmarks/symlex-common/src/L10n/phpbenchmarks.fr_FR.yml", "fr_FR", "phpbenchmarks" ] ]

controller.rest.benchmark:
class: PhpBenchmarksSymlex\Controller\BenchmarkController
public: true
arguments: [ "@serializer", "@event_dispatcher" ]
11 changes: 10 additions & 1 deletion src/Controller/BenchmarkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@
namespace PhpBenchmarksSymlex\Controller;

use PhpBenchmarksRestData\Service;
use PhpBenchmarksSymlex\EventListener\DefineLocaleEventListener;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Serializer\Serializer;

/**
* @see https://github.com/symlex/symlex#rest
*/
class BenchmarkController
{
/** @var Serializer */
protected $serializer;

public function __construct(Serializer $serializer)
/** @var EventDispatcher */
protected $eventDispatcher;

public function __construct(Serializer $serializer, EventDispatcher $eventDispatcher)
{
$this->serializer = $serializer;
$this->eventDispatcher = $eventDispatcher;
}

public function getAction()
{
$this->eventDispatcher->dispatch(DefineLocaleEventListener::EVENT_NAME);

$users = Service::getUsers();

$result = $this->serializer->normalize($users);
Expand Down
26 changes: 26 additions & 0 deletions src/EventListener/DefineLocaleEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace PhpBenchmarksSymlex\EventListener;

use Symfony\Component\Translation\TranslatorInterface;

class DefineLocaleEventListener
{
const EVENT_NAME = 'defineLocale';

/** @var TranslatorInterface */
protected $translator;

public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

public function onDefineLocale()
{
$locales = ['fr_FR', 'en_GB', 'aa_BB'];
$locale = $locales[rand(0, 2)];

$this->translator->setLocale($locale);
}
}
Loading

0 comments on commit a537c05

Please sign in to comment.