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
|
|
|
private static $typeFilters = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ParserFactory constructor. |
32
|
|
|
* |
33
|
|
|
* @param ContainerInterface $container |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ContainerInterface $container) { |
36
|
|
|
$this->container = $container; |
37
|
|
|
|
38
|
|
|
self::addTypeFilter(self::EXTENSION_FOLDER, function ($fileName) { |
39
|
|
|
return strpos($fileName, '/') === strlen($fileName) - 1; |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
self::addTypeFilter(self::EXTENSION_JSON, function ($fileName) { |
43
|
|
|
return strpos($fileName, '.json') === strlen($fileName) - 5; |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
self::addTypeFilter(self::EXTENSION_MD, function ($fileName) { |
47
|
|
|
return strpos($fileName, '.md') !== false; |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
self::addTypeFilter(self::EXTENSION_YML, function ($fileName) { |
51
|
|
|
return strpos($fileName, '.yaml') !== false || strpos($fileName, '.yml') !== false; |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
self::addTypeFilter(self::EXTENSION_IMG, function ($fileName) { |
55
|
|
|
return strpos($fileName, '.jpg') !== false || strpos($fileName, '.png') !== false; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
self::addTypeFilter(self::EXTENSION_CSS, function ($fileName) { |
59
|
|
|
return strpos($fileName, '.css') !== false; |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
self::addTypeFilter(self::EXTENSION_JS, function ($fileName) { |
63
|
|
|
return strpos($fileName, '.js') === strlen($fileName) - 3; |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
self::addTypeFilter(self::EXTENSION_SASS, function ($fileName) { |
67
|
|
|
return strpos($fileName, '.scss') !== false || strpos($fileName, '.sass') !== false; |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $type |
73
|
|
|
* @param callable $filter |
74
|
|
|
*/ |
75
|
|
|
public static function addTypeFilter(string $type, callable $filter) { |
76
|
|
|
self::$typeFilters[$type][] = $filter; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $fileName |
81
|
|
|
* |
82
|
|
|
* @return Parser|null |
83
|
|
|
*/ |
84
|
|
|
public function getByFileName($fileName) : ?Parser { |
85
|
|
|
if (!is_string($fileName)) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$type = self::PARSER_DEFAULT; |
90
|
|
|
|
91
|
|
|
foreach (self::$typeFilters as $filterType => $filters) { |
92
|
|
|
foreach ($filters as $filterCheck) { |
93
|
|
|
if ($filterCheck($fileName)) { |
94
|
|
|
$type = $filterType; |
95
|
|
|
break; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->getByType($type); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $type |
105
|
|
|
* |
106
|
|
|
* @return Parser|mixed |
107
|
|
|
* @throws ParserException |
108
|
|
|
*/ |
109
|
|
|
public function getByType($type) : Parser { |
110
|
|
|
$key = "parser.{$type}"; |
111
|
|
|
|
112
|
|
|
if (!$this->container->has($key)) { |
113
|
|
|
throw new ParserException("Parser with the key {$key} not found as a service."); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->container->get($key); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|