1 | <?php |
||
14 | class PackageXmlParser implements Parser |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var \SplFileObject The package.xml file |
||
19 | */ |
||
20 | protected $file = null; |
||
21 | |||
22 | /** |
||
23 | * @var array Map of package content types to path prefixes |
||
24 | */ |
||
25 | protected $targets = array(); |
||
26 | |||
27 | /** |
||
28 | * @param string $packageXmlFile |
||
29 | */ |
||
30 | 6 | public function __construct($packageXmlFile) |
|
34 | |||
35 | /** |
||
36 | * @return array |
||
37 | * @throws \ErrorException |
||
38 | */ |
||
39 | 5 | public function getMappings() |
|
40 | { |
||
41 | 5 | if (!$this->file->isReadable()) { |
|
42 | 1 | throw new \ErrorException(sprintf('Package file "%s" not readable', $this->file->getPathname())); |
|
43 | } |
||
44 | |||
45 | 4 | $map = $this->parseMappings(); |
|
46 | 4 | return $map; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * @throws \RuntimeException |
||
51 | * @return array |
||
52 | */ |
||
53 | 4 | protected function parseMappings() |
|
54 | { |
||
55 | 4 | $map = array(); |
|
56 | |||
57 | /** @var $package \SimpleXMLElement */ |
||
58 | 4 | $package = simplexml_load_file($this->file->getPathname()); |
|
59 | 4 | if (isset($package)) { |
|
60 | 4 | foreach ($package->xpath('//contents/target') as $target) { |
|
61 | try { |
||
62 | 4 | $basePath = $this->getTargetPath($target); |
|
63 | |||
64 | 4 | foreach ($target->children() as $child) { |
|
65 | 4 | foreach ($this->getElementPaths($child) as $elementPath) { |
|
66 | 4 | if (pathinfo($elementPath, PATHINFO_EXTENSION) == 'txt') { |
|
67 | continue; |
||
68 | } |
||
69 | 4 | $relativePath = str_replace('//', '/', $basePath . '/' . $elementPath); |
|
70 | //remove the any trailing './' or '.' from the targets base-path. |
||
71 | 4 | if (strpos($relativePath, './') === 0) { |
|
72 | 4 | $relativePath = substr($relativePath, 2); |
|
73 | 4 | } |
|
74 | 4 | $map[] = array($relativePath, $relativePath); |
|
75 | 4 | } |
|
76 | 4 | } |
|
77 | |||
78 | 4 | } catch (\RuntimeException $e) { |
|
79 | // Skip invalid targets |
||
80 | 2 | continue; |
|
81 | } |
||
82 | 4 | } |
|
83 | 4 | } |
|
84 | 4 | return $map; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param \SimpleXMLElement $target |
||
89 | * @return string |
||
90 | * @throws \RuntimeException |
||
91 | */ |
||
92 | 4 | protected function getTargetPath(\SimpleXMLElement $target) |
|
101 | |||
102 | /** |
||
103 | * @return array |
||
104 | */ |
||
105 | 4 | protected function getTargetsDefinitions() |
|
118 | |||
119 | /** |
||
120 | * @param \SimpleXMLElement $element |
||
121 | * @return array |
||
122 | * @throws \RuntimeException |
||
123 | */ |
||
124 | 4 | protected function getElementPaths(\SimpleXMLElement $element) |
|
153 | } |
||
154 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.