|
1
|
|
|
<?php namespace Arcanedev\EmbedVideo; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Arr; |
|
4
|
|
|
use Illuminate\Support\Manager; |
|
5
|
|
|
use Arcanedev\EmbedVideo\Contracts\ParserManager as ParserManagerContract; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ParserManager |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\EmbedVideo |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class ParserManager extends Manager implements ParserManagerContract |
|
14
|
|
|
{ |
|
15
|
|
|
/* ----------------------------------------------------------------- |
|
16
|
|
|
| Properties |
|
17
|
|
|
| ----------------------------------------------------------------- |
|
18
|
|
|
*/ |
|
19
|
|
|
/** |
|
20
|
|
|
* Parser's patterns. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $patterns = []; |
|
25
|
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
|
27
|
|
|
| Getters & Setters |
|
28
|
|
|
| ----------------------------------------------------------------- |
|
29
|
|
|
*/ |
|
30
|
|
|
/** |
|
31
|
|
|
* Get the default driver name. |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getDefaultDriver() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->getConfig('default'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/* ----------------------------------------------------------------- |
|
41
|
|
|
| Constructor |
|
42
|
|
|
| ----------------------------------------------------------------- |
|
43
|
|
|
*/ |
|
44
|
|
|
/** |
|
45
|
|
|
* Create a new manager instance. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \Illuminate\Foundation\Application $app |
|
48
|
|
|
*/ |
|
49
|
87 |
|
public function __construct($app) |
|
50
|
|
|
{ |
|
51
|
87 |
|
parent::__construct($app); |
|
52
|
|
|
|
|
53
|
87 |
|
$this->registerParsers(); |
|
54
|
87 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/* ----------------------------------------------------------------- |
|
57
|
|
|
| Main Methods |
|
58
|
|
|
| ----------------------------------------------------------------- |
|
59
|
|
|
*/ |
|
60
|
|
|
/** |
|
61
|
|
|
* Get a video parser implementation. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $parser |
|
64
|
|
|
* |
|
65
|
|
|
* @return \Arcanedev\EmbedVideo\Contracts\Parser |
|
66
|
|
|
*/ |
|
67
|
81 |
|
public function parser($parser = null) |
|
68
|
|
|
{ |
|
69
|
81 |
|
return $this->driver($parser); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Guess the parser based on the given url. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $url |
|
76
|
|
|
* |
|
77
|
|
|
* @return \Arcanedev\EmbedVideo\Contracts\Parser|null |
|
78
|
|
|
*/ |
|
79
|
6 |
|
public function guess($url) |
|
80
|
|
|
{ |
|
81
|
6 |
|
foreach ($this->patterns as $driver => $patterns) { |
|
82
|
6 |
|
foreach ($patterns as $pattern) { |
|
83
|
6 |
|
if (preg_match('~'.$pattern.'~imu', $url, $matches)) |
|
84
|
6 |
|
return $this->parser($driver)->setUrl($url); |
|
85
|
2 |
|
} |
|
86
|
2 |
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/* ----------------------------------------------------------------- |
|
92
|
|
|
| Other Methods |
|
93
|
|
|
| ----------------------------------------------------------------- |
|
94
|
|
|
*/ |
|
95
|
|
|
/** |
|
96
|
|
|
* Get the config repository. |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Illuminate\Contracts\Config\Repository |
|
99
|
|
|
*/ |
|
100
|
87 |
|
protected function config() |
|
101
|
|
|
{ |
|
102
|
87 |
|
return $this->app['config']; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get a config value. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $key |
|
109
|
|
|
* @param mixed|null $default |
|
110
|
|
|
* |
|
111
|
|
|
* @return mixed |
|
112
|
|
|
*/ |
|
113
|
87 |
|
protected function getConfig($key, $default = null) |
|
114
|
|
|
{ |
|
115
|
87 |
|
return $this->config()->get("embed-video.{$key}", $default); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Register the parsers. |
|
120
|
|
|
*/ |
|
121
|
87 |
|
private function registerParsers() |
|
122
|
|
|
{ |
|
123
|
87 |
|
foreach ($this->getConfig('parsers') as $driver => $configs) { |
|
124
|
87 |
|
$this->registerParser($driver, $configs); |
|
125
|
29 |
|
} |
|
126
|
87 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Register the parser. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $driver |
|
132
|
|
|
* @param array $configs |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function registerParser($driver, array $configs) |
|
135
|
|
|
{ |
|
136
|
87 |
|
$this->extend($driver, function () use ($driver, $configs) { |
|
137
|
81 |
|
return new $configs['class']( |
|
138
|
81 |
|
$configs['options'] |
|
139
|
27 |
|
); |
|
140
|
87 |
|
}); |
|
141
|
|
|
|
|
142
|
87 |
|
$this->registerPatterns($driver, $configs); |
|
143
|
87 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Register patterns. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $driver |
|
149
|
|
|
* @param array $configs |
|
150
|
|
|
*/ |
|
151
|
87 |
|
protected function registerPatterns($driver, array $configs) |
|
152
|
|
|
{ |
|
153
|
87 |
|
$this->patterns[$driver] = Arr::get($configs, 'options.patterns', []); |
|
154
|
87 |
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|