1 <?php
  2 
  3 /**
  4  * Utility class to generate absolute paths.
  5  *
  6  * @author gharlan
  7  *
  8  * @package redaxo\core
  9  */
 10 class rex_path_default_provider
 11 {
 12     protected $base;
 13     protected $backend;
 14     protected $provideAbsolutes;
 15 
 16     /**
 17      * Initializes the class.
 18      *
 19      * @param string $htdocs           Htdocs path
 20      * @param string $backend          Backend folder name
 21      * @param bool   $provideAbsolutes Flag whether to return absolute path, or relative ones
 22      */
 23     public function __construct($htdocs, $backend, $provideAbsolutes)
 24     {
 25         if ($provideAbsolutes) {
 26             $this->base = realpath($htdocs) . '/';
 27             $this->backend = $backend;
 28         } else {
 29             $this->base = $htdocs;
 30             $this->backend = substr($htdocs, -3) === '../' ? '' : $htdocs . $backend . '/';
 31         }
 32         $this->provideAbsolutes = $provideAbsolutes;
 33     }
 34 
 35     /**
 36      * Returns the base/root path.
 37      *
 38      * @param string $file File
 39      *
 40      * @return string
 41      */
 42     public function base($file)
 43     {
 44         if ($this->provideAbsolutes) {
 45             return strtr($this->base . $file, '/\\', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR);
 46         }
 47         return $this->base . $file;
 48     }
 49 
 50     /**
 51      * Returns the path to the frontend (the document root).
 52      *
 53      * @param string $file File
 54      *
 55      * @return string
 56      */
 57     public function frontend($file)
 58     {
 59         return $this->base($file);
 60     }
 61 
 62     /**
 63      * Returns the path to the frontend-controller (index.php from frontend).
 64      *
 65      * @return string
 66      */
 67     public function frontendController()
 68     {
 69         return $this->base('index.php');
 70     }
 71 
 72     /**
 73      * Returns the path to the backend (folder where the backend controller is placed).
 74      *
 75      * @param string $file File
 76      *
 77      * @return string
 78      */
 79     public function backend($file)
 80     {
 81         if ($this->provideAbsolutes) {
 82             return $this->frontend($this->backend . '/' . $file);
 83         }
 84         return $this->backend . $file;
 85     }
 86 
 87     /**
 88      * Returns the path to the backend-controller (index.php from backend).
 89      *
 90      * @return string
 91      */
 92     public function backendController()
 93     {
 94         return $this->backend('index.php');
 95     }
 96 
 97     /**
 98      * Returns the path to the media-folder.
 99      *
100      * @param string $file File
101      *
102      * @return string
103      */
104     public function media($file)
105     {
106         return $this->frontend('media/' . $file);
107     }
108 
109     /**
110      * Returns the path to the assets folder.
111      *
112      * @param string $file File
113      *
114      * @return string
115      */
116     public function assets($file)
117     {
118         return $this->frontend('assets/' . $file);
119     }
120 
121     /**
122      * Returns the path to the assets folder of the core, which contains all assets required by the core to work properly.
123      *
124      * @param string $file File
125      *
126      * @return string
127      */
128     public function coreAssets($file)
129     {
130         return $this->assets('core/' . $file);
131     }
132 
133     /**
134      * Returns the path to the assets folder of the given addon, which contains all assets required by the addon to work properly.
135      *
136      * @param string $addon Addon
137      * @param string $file  File
138      *
139      * @return string
140      *
141      * @see assets()
142      */
143     public function addonAssets($addon, $file)
144     {
145         return $this->assets('addons/' . $addon . '/' . $file);
146     }
147 
148     /**
149      * Returns the path to the assets folder of the given plugin of the given addon.
150      *
151      * @param string $addon  Addon
152      * @param string $plugin Plugin
153      * @param string $file   File
154      *
155      * @return string
156      *
157      * @see assets()
158      */
159     public function pluginAssets($addon, $plugin, $file)
160     {
161         return $this->addonAssets($addon, 'plugins/' . $plugin . '/' . $file);
162     }
163 
164     /**
165      * Returns the path to the bin folder.
166      *
167      * @param string $file File
168      *
169      * @return string
170      */
171     public function bin($file)
172     {
173         return $this->backend('bin/' . $file);
174     }
175 
176     /**
177      * Returns the path to the data folder.
178      *
179      * @param string $file File
180      *
181      * @return string
182      */
183     public function data($file)
184     {
185         return $this->backend('data/' . $file);
186     }
187 
188     /**
189      * Returns the path to the data folder of the core.
190      *
191      * @param string $file File
192      *
193      * @return string
194      */
195     public function coreData($file)
196     {
197         return $this->data('core/' . $file);
198     }
199 
200     /**
201      * Returns the path to the data folder of the given addon.
202      *
203      * @param string $addon Addon
204      * @param string $file  File
205      *
206      * @return string
207      */
208     public function addonData($addon, $file)
209     {
210         return $this->data('addons/' . $addon . '/' . $file);
211     }
212 
213     /**
214      * Returns the path to the data folder of the given plugin of the given addon.
215      *
216      * @param string $addon  Addon
217      * @param string $plugin Plugin
218      * @param string $file   File
219      *
220      * @return string
221      */
222     public function pluginData($addon, $plugin, $file)
223     {
224         return $this->addonData($addon, 'plugins/' . $plugin . '/' . $file);
225     }
226 
227     /**
228      * Returns the path to the cache folder.
229      *
230      * @param string $file File
231      *
232      * @return string
233      */
234     public function cache($file)
235     {
236         return $this->backend('cache/' . $file);
237     }
238 
239     /**
240      * Returns the path to the cache folder of the core.
241      *
242      * @param string $file File
243      *
244      * @return string
245      */
246     public function coreCache($file)
247     {
248         return $this->cache('core/' . $file);
249     }
250 
251     /**
252      * Returns the path to the cache folder of the given addon.
253      *
254      * @param string $addon Addon
255      * @param string $file  File
256      *
257      * @return string
258      */
259     public function addonCache($addon, $file)
260     {
261         return $this->cache('addons/' . $addon . '/' . $file);
262     }
263 
264     /**
265      * Returns the path to the cache folder of the given plugin.
266      *
267      * @param string $addon  Addon
268      * @param string $plugin Plugin
269      * @param string $file   File
270      *
271      * @return string
272      */
273     public function pluginCache($addon, $plugin, $file)
274     {
275         return $this->addonCache($addon, 'plugins/' . $plugin . '/' . $file);
276     }
277 
278     /**
279      * Returns the path to the src folder.
280      *
281      * @param string $file File
282      *
283      * @return string
284      */
285     public function src($file)
286     {
287         return $this->backend('src/' . $file);
288     }
289 
290     /**
291      * Returns the path to the actual core.
292      *
293      * @param string $file File
294      *
295      * @return string
296      */
297     public function core($file)
298     {
299         return $this->src('core/' . $file);
300     }
301 
302     /**
303      * Returns the base path to the folder of the given addon.
304      *
305      * @param string $addon Addon
306      * @param string $file  File
307      *
308      * @return string
309      */
310     public function addon($addon, $file)
311     {
312         return $this->src('addons/' . $addon . '/' . $file);
313     }
314 
315     /**
316      * Returns the base path to the folder of the plugin of the given addon.
317      *
318      * @param string $addon  Addon
319      * @param string $plugin Plugin
320      * @param string $file   File
321      *
322      * @return string
323      */
324     public function plugin($addon, $plugin, $file)
325     {
326         return $this->addon($addon, 'plugins/' . $plugin . '/' . $file);
327     }
328 }
329