5
5
use Snippetify \SnippetSniffer \Scrapers \ScraperInterface ;
6
6
use Snippetify \SnippetSniffer \Providers \ProviderInterface ;
7
7
8
- class SnippetSniffer
8
+ final class SnippetSniffer
9
9
{
10
10
/**
11
- * The config .
11
+ * Configuration .
12
12
*
13
13
* @var string
14
14
*/
15
- protected $ config ;
15
+ private $ config ;
16
+
17
+ /**
18
+ * Scrapers.
19
+ *
20
+ * @var array
21
+ */
22
+ private $ scrapers ;
23
+
24
+ /**
25
+ * Providers.
26
+ *
27
+ * @var array
28
+ */
29
+ private $ providers ;
16
30
17
31
/**
18
32
* Singletion.
@@ -33,7 +47,18 @@ public function __construct(array $config)
33
47
throw new \InvalidArgumentException ("Invalid arguments " );
34
48
}
35
49
36
- $ this ->config = $ config ;
50
+ $ this ->config = $ config ;
51
+ $ this ->scrapers = Core::$ scrapers ;
52
+ $ this ->providers = Core::$ providers ;
53
+
54
+ if (!empty ($ config ['scrapers ' ])) {
55
+ $ this ->scrapers = array_merge ($ this ->scrapers , $ config ['scrapers ' ]);
56
+ }
57
+
58
+ if (!empty ($ config ['providers ' ])) {
59
+ $ this ->providers = array_merge ($ this ->providers , $ config ['providers ' ]);
60
+ }
61
+
37
62
}
38
63
39
64
/**
@@ -49,6 +74,42 @@ public static function create(array $config): self
49
74
return self ::$ instance ;
50
75
}
51
76
77
+ /**
78
+ * Add scraper.
79
+ *
80
+ * @param string $name
81
+ * @param string $class
82
+ * @return self
83
+ */
84
+ public function addScraper (string $ name , string $ class ): self
85
+ {
86
+ if (empty (trim ($ name )) || empty (trim ($ class ))) {
87
+ throw new \InvalidArgumentException ("Arguments cannot be empty. " );
88
+ }
89
+
90
+ $ this ->scrapers [$ name ] = $ class ;
91
+
92
+ return $ this ;
93
+ }
94
+
95
+ /**
96
+ * Add provider.
97
+ *
98
+ * @param string $name
99
+ * @param string $class
100
+ * @return self
101
+ */
102
+ public function addProvider (string $ name , string $ class ): self
103
+ {
104
+ if (empty (trim ($ name )) || empty (trim ($ class ))) {
105
+ throw new \InvalidArgumentException ("Arguments cannot be empty. " );
106
+ }
107
+
108
+ $ this ->providers [$ name ] = $ class ;
109
+
110
+ return $ this ;
111
+ }
112
+
52
113
/**
53
114
* Fetch snippets.
54
115
*
@@ -75,22 +136,21 @@ public function fetch(string $query, array $meta = []): array
75
136
*/
76
137
protected function provider (): ProviderInterface
77
138
{
78
- $ providers = Core::$ providers ;
79
-
80
- if (!empty ($ this ->config ['providers ' ])) {
81
- foreach ($ this ->config ['providers ' ] as $ key => $ value ) {
82
- if (!class_exists ($ value )) {
83
- throw new \RuntimeException ("Provider class not exists " );
84
- }
85
- $ providers [$ key ] = $ value ;
139
+ if (empty ($ this ->providers )) {
140
+ throw new \RuntimeException ("Providers cannot be empty. " );
141
+ }
142
+
143
+ foreach ($ this ->providers as $ key => $ value ) {
144
+ if (!class_exists ($ value )) {
145
+ throw new \RuntimeException ("Provider class not exists " );
86
146
}
87
147
}
88
148
89
- if (!array_key_exists ($ this ->config ['provider ' ]['name ' ], $ providers )) {
149
+ if (!array_key_exists ($ this ->config ['provider ' ]['name ' ], $ this -> providers )) {
90
150
throw new \RuntimeException ("Provider not exists " );
91
151
}
92
152
93
- $ provider = $ providers [$ this ->config ['provider ' ]['name ' ]]::create ($ this ->config ['provider ' ]);
153
+ $ provider = $ this -> providers [$ this ->config ['provider ' ]['name ' ]]::create ($ this ->config ['provider ' ]);
94
154
95
155
if (!$ provider instanceof ProviderInterface) {
96
156
throw new \RuntimeException ("Provider class must implement the ProviderInterface " );
@@ -106,20 +166,19 @@ protected function provider(): ProviderInterface
106
166
*/
107
167
protected function scraper (string $ name ): ScraperInterface
108
168
{
109
- $ scrapers = Core::$ scrapers ;
110
-
111
- if (!empty ($ this ->config ['scrapers ' ])) {
112
- foreach ($ this ->config ['scrapers ' ] as $ key => $ value ) {
113
- if (!class_exists ($ value )) {
114
- throw new \RuntimeException ("Scraper class not exists " );
115
- }
116
- $ scrapers [$ key ] = $ value ;
169
+ if (empty ($ this ->scrapers )) {
170
+ throw new \RuntimeException ("Scrapers cannot be empty. " );
171
+ }
172
+
173
+ foreach ($ this ->scrapers as $ key => $ value ) {
174
+ if (!class_exists ($ value )) {
175
+ throw new \RuntimeException ("Scraper class not exists " );
117
176
}
118
177
}
119
178
120
- $ name = array_key_exists ($ name , $ scrapers ) ? $ name : 'default ' ;
179
+ $ name = array_key_exists ($ name , $ this -> scrapers ) ? $ name : 'default ' ;
121
180
122
- $ scraper = new $ scrapers [$ name ]($ this ->config );
181
+ $ scraper = new $ this -> scrapers [$ name ]($ this ->config );
123
182
124
183
if (!$ scraper instanceof ScraperInterface) {
125
184
throw new \RuntimeException ("Scraper class must implement the ScraperInterface " );
0 commit comments