1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LaravelYaml package. |
5
|
|
|
* |
6
|
|
|
* (c) Théo FIDRY <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Fidry\LaravelYaml\FileLoader\Parser\Yaml; |
13
|
|
|
|
14
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder; |
15
|
|
|
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException; |
16
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\ParserInterface; |
17
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ResolverInterface; |
18
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ServiceResolver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Théo FIDRY <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ImportsParser implements ParserInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Parses import statements and return resources. |
27
|
|
|
* |
28
|
|
|
* @param ContainerBuilder $containerBuilder |
29
|
|
|
* @param array $content YAML file content |
30
|
|
|
* @param string $fileName file name |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
* @throws InvalidArgumentException |
34
|
|
|
*/ |
35
|
|
|
public function parse(ContainerBuilder $containerBuilder, $content, $fileName) |
36
|
|
|
{ |
37
|
|
|
if (false === isset($content['imports'])) { |
38
|
|
|
return []; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
if (false === is_array($content['imports'])) { |
|
|
|
|
42
|
|
|
throw new InvalidArgumentException( |
43
|
|
|
sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $fileName) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$imports = []; |
48
|
|
|
foreach ($content['imports'] as $importStatement) { |
49
|
|
|
if (false === isset($importStatement['resource']) || false === is_string($importStatement['resource'])) { |
50
|
|
|
throw new InvalidArgumentException( |
51
|
|
|
sprintf('The "imports" objects should contain a "resource" key in %s. Check your YAML syntax.', $fileName) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$imports[$importStatement['resource']] = true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return array_keys($imports); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.