1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
<?php /** * Generic configuration model serializer for portable Loco configs */ abstract class Loco_config_Model { /** * Root directory for calculating relative file paths */ private string $base; /** * registry of location constants that may have been overridden */ private array $dirs;
/** * @return Iterator */ abstract public function query( $query, $context = null );
/** * @return void */ abstract public function createDom();
/** * @return DOMDocument|LocoConfigDocument */ abstract public function getDom();
/** * Super constructor for all model types */ final public function __construct(){ $this->dirs = []; $this->createDom(); $this->setDirectoryPath( loco_constant('ABSPATH') ); } /** * @return void */ public function setDirectoryPath( $path, $key = null ){ $path = untrailingslashit($path); if( is_null($key) ){ $this->base = $path; } else { $this->dirs[$key] = $path; } }
/** * Evaluate a name constant pointing to a file location * @param string|null $key one of 'LOCO_LANG_DIR', 'WP_LANG_DIR', 'WP_PLUGIN_DIR', 'WPMU_PLUGIN_DIR', 'WP_CONTENT_DIR', or 'ABSPATH' */ public function getDirectoryPath( $key = null ){ if( is_null($key) ){ $value = $this->base; } else if( isset($this->dirs[$key]) ){ $value = $this->dirs[$key]; } else { $value = untrailingslashit( loco_constant($key) ); }
return $value; }
/** * @return LocoConfigElement */ public function createFileElement( Loco_fs_File $file ){ $path = $file->getPath(); // only test concrete file type if existence is testable if( '' === $path || '/' !== $path[0] ){ $type = $file->extension() ? 'file' : 'directory'; } else { $type = $file->isDirectory() ? 'directory' : 'file'; } $node = $this->getDom()->createElement($type); // Calculate relative path to the config file itself $relpath = $file->getRelativePath($this->base); // Map to a configured base path if target is not under our root. This makes XML more portable // matching order is the most specific first, resulting in the shortest path if( $relpath && ( Loco_fs_File::abs($relpath) || '..' === substr($relpath,0,2) || $this->base === $this->getDirectoryPath('ABSPATH') ) ){ $bases = [ 'LOCO_LANG_DIR', 'WP_LANG_DIR', 'WP_PLUGIN_DIR', 'WPMU_PLUGIN_DIR', 'WP_CONTENT_DIR', 'ABSPATH' ]; foreach( $bases as $key ){ if( ( $base = $this->getDirectoryPath($key) ) && $base !== $this->base ){ $base .= '/'; $len = strlen($base); if( substr($path,0,$len) === $base ){ $node->setAttribute('base',$key); $relpath = substr( $path, $len ); break; } } } } $this->setFileElementPath($node,$relpath); return $node; }
/** * @param LocoConfigElement $node * @param string $path * @return LocoConfigText */ protected function setFileElementPath( $node, $path ){ $text = $this->getDom()->createTextNode($path); $node->appendChild($text); return $text; }
/** * @param LocoConfigElement $el * @return Loco_fs_File */ public function evaluateFileElement( $el ){ $path = $el->textContent;
switch( $el->nodeName ){ case 'directory': $file = new Loco_fs_Directory($path); break; case 'file': $file = new Loco_fs_File($path); break; case 'path': $file = new Loco_fs_File($path); if( $file->isDirectory() ){ $file = new Loco_fs_Directory($path); } break; default: throw new InvalidArgumentException('Cannot evaluate file element from <'.$el->nodeName.'>'); } if( $el->hasAttribute('base') ){ $key = $el->getAttribute('base'); $base = $this->getDirectoryPath($key); $file->normalize( $base ); } else { $file->normalize( $this->base ); } return $file; }
/** * @param LocoConfigElement $el * @param string $attr * @return bool */ public function evaluateBooleanAttribute( $el, $attr ){ if( ! $el->hasAttribute($attr) ){ return false; } $value = (string) $el->getAttribute($attr); return 'false' !== $value && 'no' !== $value && '' !== $value; }
}
|