1 <?php
2
3 namespace Skimia\Foundation\Annotations;
4
5 use Illuminate\Support\ServiceProvider;
6 use Skimia\Foundation\Support\Traits\NamespaceClassFinderTrait;
7 use Illuminate\Contracts\Foundation\Application;
8
9 abstract class BaseServiceProvider extends ServiceProvider
10 {
11 use NamespaceClassFinderTrait;
12
13 14 15 16 17
18 protected $classesToScan = [];
19
20 21 22 23 24
25 protected $scanWhenLocal = true;
26
27 28 29 30 31 32
33 protected $scanEverything = true;
34
35 36 37
38 public function __construct(Application $app)
39 {
40
41 parent::__construct($app);
42 }
43
44 45 46 47 48
49 public function register()
50 {
51 $this->registerAnnotationScanner();
52
53 if (method_exists($this, 'registerCommands')) {
54 $this->registerCommands();
55 }
56 }
57
58 59 60 61 62
63 public function boot()
64 {
65 $this->addAnnotations($this->getAnnotationScanner());
66
67 $this->loadAnnotated();
68 }
69
70 71 72 73 74
75 abstract protected function registerAnnotationScanner();
76
77 78 79
80 abstract protected function getAnnotationScanner();
81
82 83 84 85 86
87 public function addAnnotations(Scanner $scanner)
88 {
89 }
90
91 92 93 94 95
96 protected function scanAnnotations()
97 {
98 $scans = $this->classesToScan();
99
100 if (empty($scans)) {
101 return;
102 }
103
104 $scanner = $this->getAnnotationScanner();
105
106 $scanner->setClassesToScan($scans);
107
108 $scanner->scan();
109 }
110
111 112 113 114 115
116 public function loadAnnotated()
117 {
118 if ($this->app->environment('local') && $this->scanWhenLocal) {
119 $this->scanAnnotations();
120 }
121
122 $scans = $this->classesToScan();
123
124 if (! empty($scans) && $this->getAnnotationScanner()->annotationsAreScanned()) {
125 $this->getAnnotationScanner()->loadScannedAnnotations();
126 }
127 }
128
129 130 131 132 133
134 public function classesToScan()
135 {
136 if ($this->scanEverything) {
137 return $this->getAllClasses();
138 }
139
140 return $this->classesToScan;
141 }
142 }
143