Przeglądaj źródła

Create bundles for benchmarks

tags/3.1.0
Steevan BARBOYON 8 lat temu
rodzic
commit
7011c50a98

+ 15
- 0
Bundle/HelloWorldBundle/Controller/HelloWorldController.php Wyświetl plik

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\Bundle\HelloWorldBundle\Controller;
4
+
5
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
+use Symfony\Component\HttpFoundation\Response;
7
+
8
+class HelloWorldController extends Controller
9
+{
10
+    /** @return Response */
11
+    public function helloworldAction()
12
+    {
13
+        return new Response('Hello World !');
14
+    }
15
+}

+ 10
- 0
Bundle/HelloWorldBundle/HelloWorldBundle.php Wyświetl plik

@@ -0,0 +1,10 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\Bundle\HelloWorldBundle;
4
+
5
+use Symfony\Component\HttpKernel\Bundle\Bundle;
6
+
7
+class HelloWorldBundle extends Bundle
8
+{
9
+
10
+}

+ 3
- 0
Bundle/HelloWorldBundle/Resources/config/routing.yml Wyświetl plik

@@ -0,0 +1,3 @@
1
+helloworld:
2
+    path: /benchmark/helloworld
3
+    defaults: { _controller: HelloWorldBundle:HelloWorld:helloworld }

+ 20
- 0
Bundle/RestBundle/Controller/RestController.php Wyświetl plik

@@ -0,0 +1,20 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\Bundle\RestBundle\Controller;
4
+
5
+use PhpBenchmarksRestData\Service;
6
+use PhpBenchmarksSymfony\EventListener\DefineLocaleEventListener;
7
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
+use Symfony\Component\HttpFoundation\JsonResponse;
9
+use Symfony\Component\HttpFoundation\Response;
10
+
11
+class RestController extends Controller
12
+{
13
+    /** @return Response */
14
+    public function restAction()
15
+    {
16
+        $this->get('event_dispatcher')->dispatch(DefineLocaleEventListener::EVENT_NAME);
17
+
18
+        return new JsonResponse($this->get('serializer')->normalize(Service::getUsers(), 'json'));
19
+    }
20
+}

+ 21
- 0
Bundle/RestBundle/DependencyInjection/RestExtension.php Wyświetl plik

@@ -0,0 +1,21 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\Bundle\RestBundle\DependencyInjection;
4
+
5
+use Symfony\Component\Config\FileLocator;
6
+use Symfony\Component\DependencyInjection\ContainerBuilder;
7
+use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
8
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9
+
10
+class RestExtension extends Extension
11
+{
12
+    /**
13
+     * @param array $configs
14
+     * @param ContainerBuilder $container
15
+     */
16
+    public function load(array $configs, ContainerBuilder $container)
17
+    {
18
+        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
19
+        $loader->load('services.yml');
20
+    }
21
+}

+ 55
- 0
Bundle/RestBundle/Normalizer/CommentNormalizer.php Wyświetl plik

@@ -0,0 +1,55 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\Bundle\RestBundle\Normalizer;
4
+
5
+use PhpBenchmarksRestData\Comment;
6
+use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
+use Symfony\Component\Serializer\SerializerAwareInterface;
8
+use Symfony\Component\Serializer\SerializerInterface;
9
+use Symfony\Component\Translation\TranslatorInterface;
10
+
11
+class CommentNormalizer implements NormalizerInterface, SerializerAwareInterface
12
+{
13
+    /** @var SerializerInterface */
14
+    protected $serializer;
15
+
16
+    /** @var TranslatorInterface */
17
+    protected $translator;
18
+
19
+    public function __construct(TranslatorInterface $translator)
20
+    {
21
+        $this->translator = $translator;
22
+    }
23
+
24
+    /** @param SerializerInterface $serializer */
25
+    public function setSerializer(SerializerInterface $serializer)
26
+    {
27
+        $this->serializer = $serializer;
28
+    }
29
+
30
+    /**
31
+     * @param mixed $data
32
+     * @param ?string $format
33
+     * @return bool
34
+     */
35
+    public function supportsNormalization($data, $format = null)
36
+    {
37
+        return $data instanceof Comment;
38
+    }
39
+
40
+    /**
41
+     * @param Comment $object
42
+     * @param ?string $format
43
+     * @param array $context
44
+     * @return array
45
+     */
46
+    public function normalize($object, $format = null, array $context = [])
47
+    {
48
+        return [
49
+            'id' => $object->getId(),
50
+            'message' => $object->getMessage(),
51
+            'translated' => $this->translator->trans('translated.2000', [], 'phpbenchmarks'),
52
+            'type' => $this->serializer->normalize($object->getType(), $format, $context)
53
+        ];
54
+    }
55
+}

+ 43
- 0
Bundle/RestBundle/Normalizer/CommentTypeNormalizer.php Wyświetl plik

@@ -0,0 +1,43 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\Bundle\RestBundle\Normalizer;
4
+
5
+use PhpBenchmarksRestData\CommentType;
6
+use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
+use Symfony\Component\Translation\TranslatorInterface;
8
+
9
+class CommentTypeNormalizer implements NormalizerInterface
10
+{
11
+    /** @var TranslatorInterface */
12
+    protected $translator;
13
+
14
+    public function __construct(TranslatorInterface $translator)
15
+    {
16
+        $this->translator = $translator;
17
+    }
18
+
19
+    /**
20
+     * @param mixed $data
21
+     * @param ?string $format
22
+     * @return bool
23
+     */
24
+    public function supportsNormalization($data, $format = null)
25
+    {
26
+        return $data instanceof CommentType;
27
+    }
28
+
29
+    /**
30
+     * @param CommentType $object
31
+     * @param ?string $format
32
+     * @param array $context
33
+     * @return array
34
+     */
35
+    public function normalize($object, $format = null, array $context = [])
36
+    {
37
+        return [
38
+            'id' => $object->getId(),
39
+            'name' => $object->getName(),
40
+            'translated' => $this->translator->trans('translated.3000', [], 'phpbenchmarks'),
41
+        ];
42
+    }
43
+}

+ 56
- 0
Bundle/RestBundle/Normalizer/UserNormalizer.php Wyświetl plik

@@ -0,0 +1,56 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\Bundle\RestBundle\Normalizer;
4
+
5
+use PhpBenchmarksRestData\User;
6
+use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7
+use Symfony\Component\Serializer\SerializerAwareInterface;
8
+use Symfony\Component\Serializer\SerializerInterface;
9
+use Symfony\Component\Translation\TranslatorInterface;
10
+
11
+class UserNormalizer implements NormalizerInterface, SerializerAwareInterface
12
+{
13
+    /** @var SerializerInterface */
14
+    protected $serializer;
15
+
16
+    /** @var TranslatorInterface */
17
+    protected $translator;
18
+
19
+    public function __construct(TranslatorInterface $translator)
20
+    {
21
+        $this->translator = $translator;
22
+    }
23
+
24
+    /** @param SerializerInterface $serializer */
25
+    public function setSerializer(SerializerInterface $serializer)
26
+    {
27
+        $this->serializer = $serializer;
28
+    }
29
+
30
+    /**
31
+     * @param mixed $data
32
+     * @param ?string $format
33
+     * @return bool
34
+     */
35
+    public function supportsNormalization($data, $format = null)
36
+    {
37
+        return $data instanceof User;
38
+    }
39
+
40
+    /**
41
+     * @param User $object
42
+     * @param ?string $format
43
+     * @param array $context
44
+     * @return array
45
+     */
46
+    public function normalize($object, $format = null, array $context = [])
47
+    {
48
+        return [
49
+            'id' => $object->getId(),
50
+            'login' => $object->getLogin(),
51
+            'createdAt' => $object->getCreatedAt()->format('Y-m-d H:i:s'),
52
+            'translated' => $this->translator->trans('translated.1000', [], 'phpbenchmarks'),
53
+            'comments' => $this->serializer->normalize($object->getComments(), $format, $context)
54
+        ];
55
+    }
56
+}

+ 1500
- 0
Bundle/RestBundle/Resources/config/routing.yml
Plik diff jest za duży
Wyświetl plik


+ 27
- 0
Bundle/RestBundle/Resources/config/services.yml Wyświetl plik

@@ -0,0 +1,27 @@
1
+services:
2
+    benchmark.normalizer.user:
3
+        class: PhpBenchmarksSymfony\Bundle\RestBundle\Normalizer\UserNormalizer
4
+        public: false
5
+        arguments: ['@translator']
6
+        tags:
7
+            - { name: serializer.normalizer }
8
+
9
+    benchmark.normalizer.comment:
10
+        class: PhpBenchmarksSymfony\Bundle\RestBundle\Normalizer\CommentNormalizer
11
+        public: false
12
+        arguments: ['@translator']
13
+        tags:
14
+            - { name: serializer.normalizer }
15
+
16
+    benchmark.normalizer.comment_type:
17
+        class: PhpBenchmarksSymfony\Bundle\RestBundle\Normalizer\CommentTypeNormalizer
18
+        public: false
19
+        arguments: ['@translator']
20
+        tags:
21
+            - { name: serializer.normalizer }
22
+
23
+    benchmark.event_listener.define_locale:
24
+        class: PhpBenchmarksSymfony\EventListener\DefineLocaleEventListener
25
+        arguments: ['@request_stack', '@translator']
26
+        tags:
27
+            - { name: kernel.event_listener, event: defineLocale }

+ 17
- 0
Bundle/RestBundle/RestBundle.php Wyświetl plik

@@ -0,0 +1,17 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\Bundle\RestBundle;
4
+
5
+use PhpBenchmarksSymfony\DependencyInjection\CompilerPass\AddTranslationsPass;
6
+use Symfony\Component\DependencyInjection\ContainerBuilder;
7
+use Symfony\Component\HttpKernel\Bundle\Bundle;
8
+
9
+class RestBundle extends Bundle
10
+{
11
+    public function build(ContainerBuilder $container)
12
+    {
13
+        parent::build($container);
14
+
15
+        $container->addCompilerPass(new AddTranslationsPass());
16
+    }
17
+}

+ 26
- 0
DependencyInjection/CompilerPass/AddTranslationsPass.php Wyświetl plik

@@ -0,0 +1,26 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\DependencyInjection\CompilerPass;
4
+
5
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
+use Symfony\Component\DependencyInjection\ContainerBuilder;
7
+
8
+class AddTranslationsPass implements CompilerPassInterface
9
+{
10
+    public function process(ContainerBuilder $container)
11
+    {
12
+        $definition = $container->findDefinition('translator');
13
+        $definition->addMethodCall(
14
+            'addResource',
15
+            ['yml', __DIR__ . '/../../Translation/phpbenchmarks.en.yml', 'en', 'phpbenchmarks']
16
+        );
17
+        $definition->addMethodCall(
18
+            'addResource',
19
+            ['yml', __DIR__ . '/../../Translation/phpbenchmarks.en_GB.yml', 'en_GB', 'phpbenchmarks']
20
+        );
21
+        $definition->addMethodCall(
22
+            'addResource',
23
+            ['yml', __DIR__ . '/../../Translation/phpbenchmarks.fr_FR.yml', 'fr_FR', 'phpbenchmarks']
24
+        );
25
+    }
26
+}

+ 32
- 0
EventListener/DefineLocaleEventListener.php Wyświetl plik

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+namespace PhpBenchmarksSymfony\EventListener;
4
+
5
+use Symfony\Component\HttpFoundation\RequestStack;
6
+use Symfony\Component\Translation\TranslatorInterface;
7
+
8
+class DefineLocaleEventListener
9
+{
10
+    const EVENT_NAME = 'defineLocale';
11
+
12
+    /** @var RequestStack */
13
+    protected $requestStack;
14
+
15
+    /** @var TranslatorInterface */
16
+    protected $translator;
17
+
18
+    public function __construct(RequestStack $requestStack, TranslatorInterface $translator)
19
+    {
20
+        $this->requestStack = $requestStack;
21
+        $this->translator = $translator;
22
+    }
23
+
24
+    public function onDefineLocale()
25
+    {
26
+        $locales = ['fr_FR', 'en_GB', 'aa_BB'];
27
+        $locale = $locales[rand(0, 2)];
28
+
29
+        $this->requestStack->getCurrentRequest()->setLocale($locale);
30
+        $this->translator->setLocale($locale);
31
+    }
32
+}

+ 5000
- 0
Translation/phpbenchmarks.en.yml
Plik diff jest za duży
Wyświetl plik


+ 5000
- 0
Translation/phpbenchmarks.en_GB.yml
Plik diff jest za duży
Wyświetl plik


+ 5000
- 0
Translation/phpbenchmarks.fr_FR.yml
Plik diff jest za duży
Wyświetl plik


+ 2
- 1
composer.json Wyświetl plik

@@ -8,6 +8,7 @@
8 8
         }
9 9
     },
10 10
     "require": {
11
-        "php": ">=5.4.0"
11
+        "php": ">=5.4.0",
12
+        "phpbenchmarks/benchmark-rest-data": "1.0.0"
12 13
     }
13 14
 }

Ładowanie…
Anuluj
Zapisz