This repository was archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathYamlGameLoader.php
More file actions
72 lines (62 loc) · 1.78 KB
/
YamlGameLoader.php
File metadata and controls
72 lines (62 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/*
* This file is part of the SDPHP php-micrork Package.
* For the full copyright and license information,
* please view the LICENSE file that was distributed
* with this source code.
*/
namespace SDPHP\PHPMicrork\Loader;
include 'XML2Array.php';
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Yaml\Yaml;
use LSS\XML2Array;
/**
* YamlLevelLoader - Description.
*
* @author Juan Manuel Torres <kinojman@gmail.com>
*/
class YamlGameLoader extends FileLoader
{
/**
* @param mixed $resource
* @return array
*/
private function parse($resource)
{
$file_type = pathinfo($resource, PATHINFO_EXTENSION);
switch ($file_type) {
case 'yml':
return Yaml::parse($resource);
break;
case 'xml':
$xml_string = implode("", file($resource));
$xml_array = XML2Array::createArray($xml_string);
$root_tag = key($xml_array);
return $xml_array[$root_tag];
break;
}
}
/**
* @param mixed $resource
* @param null $type
* @return array;
*/
public function load($resource, $type = null)
{
$config = $this->parse($this->locator->locate($resource, null, true));
if (isset($config['world']['level_info'])) {
// if is world configuration load levels
$levels = [];
foreach($config['world']['level_info'] as $levelName => $levelPath) {
$levels['levels'][$levelName] = $this->parse($this->locator->locate($levelPath, null, true));
}
$config = array_merge($config, $levels);
}
return $config;
}
public function supports($resource, $type = null)
{
$file_type = pathinfo($resource, PATHINFO_EXTENSION);
return is_string($resource) && ('yml' === $file_type || 'xml' === $file_type );
}
}