Total Complexity | 10 |
Total Lines | 81 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
6 | class Theme |
||
7 | { |
||
8 | /** |
||
9 | * @var string |
||
10 | */ |
||
11 | private $path; |
||
12 | /** |
||
13 | * @var string[] |
||
14 | */ |
||
15 | private $zones; |
||
16 | |||
17 | /** |
||
18 | * @param string[] $zones |
||
19 | */ |
||
20 | public function __construct(string $path, array $zones) |
||
24 | } |
||
25 | |||
26 | public static function fromDirectory(string $path): self |
||
27 | { |
||
28 | if (!is_dir($path)) { |
||
29 | throw new UnableToLoadFileException('Cannot find directory '.$path); |
||
30 | } |
||
31 | $path = rtrim($path, '/\\'); |
||
32 | $configPath = $path.'/config.yml'; |
||
33 | if (!is_file($configPath)) { |
||
34 | throw new UnableToLoadFileException('Cannot find config.yml in theme '.$path); |
||
35 | } |
||
36 | |||
37 | $yaml = Yaml::parse(file_get_contents($configPath)); |
||
38 | |||
39 | $compulsoryFields = ['zones']; |
||
40 | |||
41 | foreach ($compulsoryFields as $field) { |
||
42 | if (!isset($yaml[$field])) { |
||
43 | throw new UnableToLoadFileException('Missing field '.$field.' in YAML file '.$configPath); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | return new self( |
||
48 | $path, |
||
49 | $yaml['zones'] |
||
50 | ); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Returns true if $path is a theme directory. |
||
55 | * |
||
56 | * @param string $path The path, with no trailing / |
||
57 | * @return bool |
||
58 | */ |
||
59 | public static function existsInDirectory(string $path): bool |
||
60 | { |
||
61 | $configPath = $path.'/config.yml'; |
||
62 | return file_exists($configPath); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getName(): string |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | public function getPath(): string |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return string[] |
||
83 | */ |
||
84 | public function getZones(): array |
||
87 | } |
||
88 | } |
||
89 |