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