|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Exception\ParserException; |
|
6
|
|
|
use Brendt\Stitcher\Parser\Parser; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ParserFactory |
|
10
|
|
|
{ |
|
11
|
|
|
const EXTENSION_JSON = 'json'; |
|
12
|
|
|
const EXTENSION_MD = 'markdown'; |
|
13
|
|
|
const EXTENSION_FOLDER = 'folder'; |
|
14
|
|
|
const EXTENSION_YML = 'yaml'; |
|
15
|
|
|
const EXTENSION_YAML = 'yaml'; |
|
16
|
|
|
const EXTENSION_IMG = 'image'; |
|
17
|
|
|
const EXTENSION_JS = 'file'; |
|
18
|
|
|
const EXTENSION_CSS = 'file'; |
|
19
|
|
|
const EXTENSION_SASS = 'sass'; |
|
20
|
|
|
const EXTENSION_SCSS = 'sass'; |
|
21
|
|
|
const PARSER_DEFAULT = 'default'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ContainerInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $container; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $typeFilters = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* ParserFactory constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param ContainerInterface $container |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(ContainerInterface $container) { |
|
39
|
|
|
$this->container = $container; |
|
40
|
|
|
|
|
41
|
|
|
self::addTypeFilter(self::EXTENSION_FOLDER, function ($fileName) { |
|
42
|
|
|
return strpos($fileName, '/') === strlen($fileName) - 1; |
|
43
|
|
|
}); |
|
44
|
|
|
|
|
45
|
|
|
self::addTypeFilter(self::EXTENSION_JSON, function ($fileName) { |
|
46
|
|
|
return strpos($fileName, '.json') === strlen($fileName) - 5; |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
self::addTypeFilter(self::EXTENSION_MD, function ($fileName) { |
|
50
|
|
|
return strpos($fileName, '.md') !== false; |
|
51
|
|
|
}); |
|
52
|
|
|
|
|
53
|
|
|
self::addTypeFilter(self::EXTENSION_YML, function ($fileName) { |
|
54
|
|
|
return strpos($fileName, '.yaml') !== false || strpos($fileName, '.yml') !== false; |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
self::addTypeFilter(self::EXTENSION_IMG, function ($fileName) { |
|
58
|
|
|
return strpos($fileName, '.jpg') !== false || strpos($fileName, '.png') !== false; |
|
59
|
|
|
}); |
|
60
|
|
|
|
|
61
|
|
|
self::addTypeFilter(self::EXTENSION_CSS, function ($fileName) { |
|
62
|
|
|
return strpos($fileName, '.css') !== false; |
|
63
|
|
|
}); |
|
64
|
|
|
|
|
65
|
|
|
self::addTypeFilter(self::EXTENSION_JS, function ($fileName) { |
|
66
|
|
|
return strpos($fileName, '.js') === strlen($fileName) - 3; |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
self::addTypeFilter(self::EXTENSION_SASS, function ($fileName) { |
|
70
|
|
|
return strpos($fileName, '.scss') !== false || strpos($fileName, '.sass') !== false; |
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $type |
|
76
|
|
|
* @param callable $filter |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function addTypeFilter(string $type, callable $filter) { |
|
79
|
|
|
self::$typeFilters[$type][] = $filter; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $fileName |
|
84
|
|
|
* |
|
85
|
|
|
* @return Parser|null |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getByFileName($fileName) : ?Parser { |
|
88
|
|
|
if (!is_string($fileName)) { |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$type = self::PARSER_DEFAULT; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @var string $filterType |
|
96
|
|
|
* @var callable[] $filters |
|
97
|
|
|
*/ |
|
98
|
|
|
foreach (self::$typeFilters as $filterType => $filters) { |
|
99
|
|
|
foreach ($filters as $filterCheck) { |
|
100
|
|
|
if ($filterCheck($fileName)) { |
|
101
|
|
|
$type = $filterType; |
|
102
|
|
|
break; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->getByType($type); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $type |
|
112
|
|
|
* |
|
113
|
|
|
* @return Parser|mixed |
|
114
|
|
|
* @throws ParserException |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getByType($type) : Parser { |
|
117
|
|
|
$key = "parser.{$type}"; |
|
118
|
|
|
|
|
119
|
|
|
if (!$this->container->has($key)) { |
|
120
|
|
|
throw new ParserException("Parser with the key {$key} not found as a service."); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this->container->get($key); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|