Completed
Branch new-addon-api (b7bde0)
by
unknown
18:29 queued 08:41
created

DomainBase::pluginFileObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain;
4
5
use DomainException;
6
use EventEspresso\core\domain\values\FilePath;
7
use EventEspresso\core\domain\values\Version;
8
9
/**
10
 * DomainBase Class
11
 * A container for all domain data related to the plugin
12
 *
13
 * @package EventEspresso\core\domain
14
 * @author  Darren Ethier
15
 * @since   4.9.38
16
 */
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
142
    {
143
        return (string) $this->plugin_file;
144
    }
145
146
147
    /**
148
     * @return FilePath
149
     */
150
    public function pluginFileObject(): FilePath
151
    {
152
        return $this->plugin_file;
153
    }
154
155
156
    /**
157
     * @return string
158
     */
159
    public function pluginBasename(): string
160
    {
161
        return $this->plugin_basename;
162
    }
163
164
165
    /**
166
     * @param string $additional_path
167
     * @return string
168
     */
169
    public function pluginPath($additional_path = ''): string
170
    {
171
        return is_string($additional_path) && $additional_path !== ''
172
            ? $this->plugin_path . $additional_path
173
            : $this->plugin_path;
174
    }
175
176
177
    /**
178
     * @param string $additional_path
179
     * @return string
180
     */
181
    public function pluginUrl($additional_path = ''): string
182
    {
183
        return is_string($additional_path) && $additional_path !== ''
184
            ? $this->plugin_url . $additional_path
185
            : $this->plugin_url;
186
    }
187
188
189
    /**
190
     * @return string
191
     */
192
    public function version(): string
193
    {
194
        return (string) $this->version;
195
    }
196
197
198
    /**
199
     * @return Version
200
     */
201
    public function versionValueObject()
202
    {
203
        return $this->version;
204
    }
205
206
207
    /**
208
     * @return string
209
     */
210
    public function distributionAssetsFolder(): string
211
    {
212
        return DomainBase::ASSETS_FOLDER;
213
    }
214
215
216
    /**
217
     * @param string $additional_path
218
     * @return string
219
     */
220
    public function distributionAssetsPath($additional_path = ''): string
221
    {
222
        return is_string($additional_path) && $additional_path !== ''
223
            ? $this->assets_path . $additional_path
224
            : $this->assets_path;
225
    }
226
227
228
    /**
229
     * @param string $additional_path
230
     * @return string
231
     */
232
    public function distributionAssetsUrl($additional_path = ''): string
233
    {
234
        return is_string($additional_path) && $additional_path !== ''
235
            ? $this->plugin_url . DomainBase::ASSETS_FOLDER . $additional_path
236
            : $this->plugin_url . DomainBase::ASSETS_FOLDER;
237
    }
238
239
240
    /**
241
     * @return string
242
     */
243
    public function assetNamespace(): string
244
    {
245
        return $this->asset_namespace;
246
    }
247
}
248