1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFeature\Service; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Config\Repository; |
7
|
|
|
use LaravelFeature\Domain\FeatureManager; |
8
|
|
|
|
9
|
|
|
class FeaturesViewScanner |
10
|
|
|
{ |
11
|
|
|
/** @var FeatureManager */ |
12
|
|
|
private $featureManager; |
13
|
|
|
|
14
|
|
|
/** @var Repository */ |
15
|
|
|
private $config; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* FeaturesViewScanner constructor. |
19
|
|
|
* |
20
|
|
|
* @param FeatureManager $featureManager |
21
|
|
|
* @param Repository $config |
22
|
3 |
|
*/ |
23
|
|
|
public function __construct(FeatureManager $featureManager, Repository $config) |
24
|
3 |
|
{ |
25
|
3 |
|
$this->featureManager = $featureManager; |
26
|
3 |
|
$this->config = $config; |
27
|
|
|
} |
28
|
3 |
|
|
29
|
|
|
public function scan() |
30
|
3 |
|
{ |
31
|
|
|
$pathsToBeScanned = $this->config->get('features.scanned_paths', [ 'resources/views' ]); |
32
|
3 |
|
|
33
|
|
|
$foundDirectives = []; |
34
|
3 |
|
|
35
|
3 |
|
foreach ($pathsToBeScanned as $path) { |
36
|
|
|
$views = $this->getAllBladeViewsInPath($path); |
37
|
3 |
|
|
38
|
3 |
|
foreach ($views as $view) { |
39
|
3 |
|
$foundDirectives = array_merge($foundDirectives, $this->getFeaturesForView($view)); |
40
|
3 |
|
} |
41
|
|
|
} |
42
|
3 |
|
|
43
|
|
|
$foundDirectives = array_unique($foundDirectives); |
44
|
3 |
|
|
45
|
3 |
|
foreach ($foundDirectives as $directive) { |
46
|
3 |
|
$this->featureManager->add($directive, $this->config->get('features.scanned_default_enabled')); |
47
|
|
|
} |
48
|
3 |
|
|
49
|
|
|
return $foundDirectives; |
50
|
|
|
} |
51
|
3 |
|
|
52
|
|
|
private function getAllBladeViewsInPath($path) |
53
|
3 |
|
{ |
54
|
3 |
|
$files = scandir($path); |
55
|
|
|
$files = array_diff($files, ['..', '.']); |
56
|
3 |
|
|
57
|
|
|
$bladeViews = []; |
58
|
3 |
|
|
59
|
3 |
|
foreach ($files as $file) { |
60
|
|
|
$itemPath = $path . DIRECTORY_SEPARATOR . $file; |
61
|
3 |
|
|
62
|
3 |
|
if (is_dir($itemPath)) { |
63
|
3 |
|
$bladeViews = array_merge($bladeViews, $this->getAllBladeViewsInPath($itemPath)); |
64
|
|
|
} |
65
|
3 |
|
|
66
|
3 |
|
if (is_file($itemPath) && Str::endsWith($file, '.blade.php')) { |
67
|
3 |
|
$bladeViews[] = $itemPath; |
68
|
3 |
|
} |
69
|
|
|
} |
70
|
3 |
|
|
71
|
|
|
return $bladeViews; |
72
|
|
|
} |
73
|
3 |
|
|
74
|
|
|
private function getFeaturesForView($view) |
75
|
3 |
|
{ |
76
|
3 |
|
$fileContents = file_get_contents($view); |
77
|
|
|
|
78
|
3 |
|
preg_match_all('/@feature\(["\'](.+)["\']\)|@featurefor\(["\'](.+)["\']\,.*\)/', $fileContents, $results); |
79
|
3 |
|
|
80
|
3 |
|
return collect($results[1]) |
81
|
3 |
|
->merge($results[2]) |
82
|
3 |
|
->filter() |
83
|
3 |
|
->toArray(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|