1 | <?php # -*- coding: utf-8 -*- |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace MultisiteGlobalMedia; |
||
5 | |||
6 | /** |
||
7 | * @method string basename() |
||
8 | * @method string dirPath() |
||
9 | * @method string dirUrl() |
||
10 | * @method string filePath() |
||
11 | * @method string name() |
||
12 | * @method string website() |
||
13 | * @method string version() |
||
14 | * @method string textDomain() |
||
15 | * @method string textDomainPath() |
||
16 | */ |
||
17 | class PluginProperties implements \ArrayAccess |
||
18 | { |
||
19 | const BASENAME = 'basename'; |
||
20 | const DIR_PATH = 'dirPath'; |
||
21 | const DIR_URL = 'dirUrl'; |
||
22 | const FILE_PATH = 'filePath'; |
||
23 | const NAME = 'name'; |
||
24 | const WEBSITE = 'website'; |
||
25 | const VERSION = 'version'; |
||
26 | const TEXT_DOMAIN = 'textDomain'; |
||
27 | const TEXT_DOMAIN_PATH = 'textDomainPath'; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $properties; |
||
33 | |||
34 | /** |
||
35 | * @param string $pluginFilePath |
||
36 | */ |
||
37 | public function __construct(string $pluginFilePath) |
||
38 | { |
||
39 | if (\is_array($this->properties)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
40 | return; |
||
41 | } |
||
42 | |||
43 | $fileData = [ |
||
44 | self::BASENAME => plugin_basename($pluginFilePath), |
||
45 | self::DIR_PATH => untrailingslashit(plugin_dir_path($pluginFilePath)), |
||
46 | self::DIR_URL => untrailingslashit(plugins_url('/', $pluginFilePath)), |
||
47 | self::FILE_PATH => $pluginFilePath, |
||
48 | ]; |
||
49 | |||
50 | $headerData = get_file_data( |
||
51 | $pluginFilePath, |
||
52 | [ |
||
53 | self::NAME => 'Plugin Name', |
||
54 | self::WEBSITE => 'Plugin URI', |
||
55 | self::VERSION => 'Version', |
||
56 | self::TEXT_DOMAIN => 'Text Domain', |
||
57 | self::TEXT_DOMAIN_PATH => 'Domain Path', |
||
58 | ] |
||
59 | ); |
||
60 | |||
61 | $this->properties = array_map('\strval', array_merge($fileData, $headerData)); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param string $name |
||
66 | * @param array $args |
||
67 | * @return string |
||
68 | */ |
||
69 | public function __call(string $name, array $args = []): string |
||
70 | { |
||
71 | if (!array_key_exists($name, $this->properties)) { |
||
72 | throw new \Error( |
||
73 | sprintf( |
||
74 | 'Call to undefined method %s::%s()', |
||
75 | __CLASS__, |
||
76 | $name |
||
77 | ) |
||
78 | ); |
||
79 | } |
||
80 | |||
81 | return $this->properties[$name]; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Checks if a property with the given name exists. |
||
86 | * |
||
87 | * @param string $name |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function offsetExists($name): bool |
||
91 | { |
||
92 | return array_key_exists($name, $this->properties); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Returns the value of the property with the given name. |
||
97 | * |
||
98 | * @param string $offset |
||
99 | * @return mixed |
||
100 | * @throws \OutOfRangeException If there is no property with the given name. |
||
101 | */ |
||
102 | public function offsetGet($offset) |
||
103 | { |
||
104 | if (!$this->offsetExists($offset)) { |
||
105 | throw new \OutOfRangeException("'{$offset}' is not a valid plugin property."); |
||
106 | } |
||
107 | |||
108 | return $this->properties[$offset]; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Disabled. |
||
113 | * |
||
114 | * @inheritdoc |
||
115 | * |
||
116 | * @throws \BadMethodCallException |
||
117 | */ |
||
118 | public function offsetSet($offset, $value) |
||
119 | { |
||
120 | throw new \BadMethodCallException( |
||
121 | __METHOD__ . ' is not allowed. ' . __CLASS__ . ' is read only.' |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Disabled. |
||
127 | * |
||
128 | * @inheritdoc |
||
129 | * |
||
130 | * @throws \BadMethodCallException |
||
131 | */ |
||
132 | public function offsetUnset($offset) |
||
133 | { |
||
134 | throw new \BadMethodCallException( |
||
135 | __METHOD__ . ' is not allowed. ' . __CLASS__ . ' is read only.' |
||
136 | ); |
||
137 | } |
||
138 | } |
||
139 |