1 | <?php |
||
17 | abstract class DomainBase implements DomainInterface |
||
18 | { |
||
19 | |||
20 | const ASSETS_FOLDER = 'assets/'; |
||
21 | |||
22 | /** |
||
23 | * Equivalent to `__FILE__` for main plugin file. |
||
24 | * |
||
25 | * @var FilePath |
||
26 | */ |
||
27 | private $plugin_file; |
||
28 | |||
29 | /** |
||
30 | * String indicating version for plugin |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $version; |
||
35 | |||
36 | /** |
||
37 | * @var string $plugin_basename |
||
38 | */ |
||
39 | private $plugin_basename; |
||
40 | |||
41 | /** |
||
42 | * @var string $plugin_path |
||
43 | */ |
||
44 | private $plugin_path; |
||
45 | |||
46 | /** |
||
47 | * @var string $plugin_url |
||
48 | */ |
||
49 | private $plugin_url; |
||
50 | |||
51 | /** |
||
52 | * @var string $asset_namespace |
||
53 | */ |
||
54 | private $asset_namespace; |
||
55 | |||
56 | /** |
||
57 | * @var string $assets_path |
||
58 | */ |
||
59 | private $assets_path; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | protected $initialized = false; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Initializes internal properties. |
||
69 | * |
||
70 | * @param FilePath $plugin_file |
||
71 | * @param Version $version |
||
72 | * @param string $asset_namespace |
||
73 | */ |
||
74 | public function __construct( |
||
75 | FilePath $plugin_file, |
||
76 | Version $version, |
||
77 | string $asset_namespace = Domain::ASSET_NAMESPACE |
||
78 | ) { |
||
79 | $this->plugin_file = $plugin_file; |
||
80 | $this->version = $version; |
||
81 | $this->initialize($asset_namespace); |
||
82 | } |
||
83 | |||
84 | |||
85 | /** |
||
86 | * @param string $asset_namespace |
||
87 | * @return void |
||
88 | * @since $VID:$ |
||
89 | */ |
||
90 | public function initialize($asset_namespace = Domain::ASSET_NAMESPACE) |
||
91 | { |
||
92 | if (! $this->initialized) { |
||
93 | $this->plugin_basename = plugin_basename($this->pluginFile()); |
||
94 | $this->plugin_path = plugin_dir_path($this->pluginFile()); |
||
95 | $this->plugin_url = plugin_dir_url($this->pluginFile()); |
||
96 | $this->setAssetNamespace($asset_namespace); |
||
97 | $this->setDistributionAssetsPath(); |
||
98 | $this->initialized = true; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | |||
103 | /** |
||
104 | * @param string $asset_namespace |
||
105 | * @return void |
||
106 | */ |
||
107 | public function setAssetNamespace($asset_namespace = Domain::ASSET_NAMESPACE) |
||
108 | { |
||
109 | if (! $this->asset_namespace) { |
||
110 | $this->asset_namespace = sanitize_key( |
||
111 | // convert directory separators to dashes and remove file extension |
||
112 | str_replace(['/', '.php'], ['-', ''], $asset_namespace) |
||
113 | ); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | |||
118 | /** |
||
119 | * @throws DomainException |
||
120 | * @since $VID:$ |
||
121 | */ |
||
122 | private function setDistributionAssetsPath() |
||
123 | { |
||
124 | $assets_folder_paths = [ |
||
125 | $this->plugin_path . DomainBase::ASSETS_FOLDER, |
||
126 | $this->plugin_path . 'src/' . DomainBase::ASSETS_FOLDER, |
||
127 | ]; |
||
128 | foreach ($assets_folder_paths as $assets_folder_path) { |
||
129 | if (is_readable($assets_folder_path)) { |
||
130 | $this->assets_path = trailingslashit($assets_folder_path); |
||
131 | // once we find a valid path, just break out of loop |
||
132 | break; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | public function pluginFile(): string |
||
145 | |||
146 | |||
147 | /** |
||
148 | * @return FilePath |
||
149 | */ |
||
150 | public function pluginFileObject(): FilePath |
||
154 | |||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | public function pluginBasename(): string |
||
163 | |||
164 | |||
165 | /** |
||
166 | * @param string $additional_path |
||
167 | * @return string |
||
168 | */ |
||
169 | public function pluginPath($additional_path = ''): string |
||
175 | |||
176 | |||
177 | /** |
||
178 | * @param string $additional_path |
||
179 | * @return string |
||
180 | */ |
||
181 | public function pluginUrl($additional_path = ''): string |
||
187 | |||
188 | |||
189 | /** |
||
190 | * @return string |
||
191 | */ |
||
192 | public function version(): string |
||
196 | |||
197 | |||
198 | /** |
||
199 | * @return Version |
||
200 | */ |
||
201 | public function versionValueObject() |
||
205 | |||
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | public function distributionAssetsFolder(): string |
||
214 | |||
215 | |||
216 | /** |
||
217 | * @param string $additional_path |
||
218 | * @return string |
||
219 | */ |
||
220 | public function distributionAssetsPath($additional_path = ''): string |
||
226 | |||
227 | |||
228 | /** |
||
229 | * @param string $additional_path |
||
230 | * @return string |
||
231 | */ |
||
232 | public function distributionAssetsUrl($additional_path = ''): string |
||
238 | |||
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | public function assetNamespace(): string |
||
247 | } |
||
248 |