Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Context / Reader / TranslatableContextReader.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Context\Reader;
12
13 use Behat\Behat\Context\Environment\ContextEnvironment;
14 use Behat\Behat\Context\Exception\UnknownTranslationResourceException;
15 use Behat\Behat\Context\TranslatableContext;
16 use Symfony\Component\Translation\Translator;
17
18 /**
19  * Reads translation resources from translatable contexts.
20  *
21  * @author Konstantin Kudryashov <ever.zet@gmail.com>
22  */
23 final class TranslatableContextReader implements ContextReader
24 {
25     /**
26      * @var Translator
27      */
28     private $translator;
29
30     /**
31      * Initializes loader.
32      *
33      * @param Translator $translator
34      */
35     public function __construct(Translator $translator)
36     {
37         $this->translator = $translator;
38     }
39
40     /**
41      * {@inheritdoc}
42      *
43      * @see TranslatableContext
44      */
45     public function readContextCallees(ContextEnvironment $environment, $contextClass)
46     {
47         $reflClass = new \ReflectionClass($contextClass);
48
49         if (!$reflClass->implementsInterface('Behat\Behat\Context\TranslatableContext')) {
50             return array();
51         }
52
53         $assetsId = $environment->getSuite()->getName();
54         foreach (call_user_func(array($contextClass, 'getTranslationResources')) as $path) {
55             $this->addTranslationResource($path, $assetsId);
56         }
57
58         return array();
59     }
60
61     /**
62      * Adds translation resource.
63      *
64      * @param string $path
65      * @param string $assetsId
66      *
67      * @throws UnknownTranslationResourceException
68      */
69     private function addTranslationResource($path, $assetsId)
70     {
71         switch ($ext = pathinfo($path, PATHINFO_EXTENSION)) {
72             case 'yml':
73                 $this->addTranslatorResource('yaml', $path, basename($path, '.' . $ext), $assetsId);
74                 break;
75             case 'xliff':
76                 $this->addTranslatorResource('xliff', $path, basename($path, '.' . $ext), $assetsId);
77                 break;
78             case 'php':
79                 $this->addTranslatorResource('php', $path, basename($path, '.' . $ext), $assetsId);
80                 break;
81             default:
82                 throw new UnknownTranslationResourceException(sprintf(
83                     'Can not read translations from `%s`. File type is not supported.',
84                     $path
85                 ), $path);
86         }
87     }
88
89     /**
90      * Adds resource to translator instance.
91      *
92      * @param string $type
93      * @param string $path
94      * @param string $language
95      * @param string $assetsId
96      */
97     private function addTranslatorResource($type, $path, $language, $assetsId)
98     {
99         $this->translator->addResource($type, $path, $language, $assetsId);
100     }
101 }