1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
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 Cubiche\Core\Metadata\Driver; |
12
|
|
|
|
13
|
|
|
use Cubiche\Core\Metadata\ClassMetadata; |
14
|
|
|
use Cubiche\Core\Metadata\ClassMetadataInterface; |
15
|
|
|
use Cubiche\Core\Metadata\Exception\MappingException; |
16
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
17
|
|
|
use Symfony\Component\Yaml\Yaml; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* AbstractYamlDriver class. |
21
|
|
|
* |
22
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractYamlDriver extends AbstractFileDriver |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $className; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
protected function loadMetadataFromFile($className, $file) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this->className = $className; |
37
|
|
|
$config = $this->getElement($className, $file); |
38
|
|
|
|
39
|
|
|
try { |
40
|
|
|
$classMetadata = new ClassMetadata($className); |
41
|
|
|
} catch (\ReflectionException $e) { |
42
|
|
|
throw MappingException::classNotFound($className); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->addMetadataFor($config, $classMetadata); |
46
|
|
|
|
47
|
|
|
return $classMetadata; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected function loadMappingFile($file) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
return Yaml::parse(file_get_contents($file)); |
|
|
|
|
57
|
|
|
} catch (ParseException $e) { |
58
|
|
|
throw MappingException::invalidMapping($this->className, $file); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
protected function getExtension() |
66
|
|
|
{ |
67
|
|
|
return '.yml'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $config |
72
|
|
|
* @param ClassMetadataInterface $classMetadata |
73
|
|
|
*/ |
74
|
|
|
abstract protected function addMetadataFor(array $config, ClassMetadataInterface $classMetadata); |
75
|
|
|
} |
76
|
|
|
|
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.