convert to autocracy
[mdref/mdref] / mdref / Finder.php
1 <?php
2
3 namespace mdref;
4
5 class Finder
6 {
7 /**
8 * Base URL
9 * @var \http\Controller\Url
10 */
11 protected $baseUrl;
12
13 /**
14 * Reference paths
15 * @var array
16 */
17 protected $refs = array();
18
19 /**
20 * @param \http\Controller\Url $baseUrl
21 * @param mixed $paths array or string of paths with markdown references
22 */
23 function __construct(\http\Controller\Url $baseUrl, $paths = ".") {
24 if (!is_array($paths)) {
25 $paths = explode(PATH_SEPARATOR, $paths);
26 }
27 $this->refs = $paths;
28 $this->baseUrl = $baseUrl;
29 }
30
31 /**
32 * @param \http\Url $requestUrl
33 * @return Path
34 */
35 function find(\http\Url $requestUrl) {
36 $file = implode(DIRECTORY_SEPARATOR,
37 $this->baseUrl->params($requestUrl));
38
39 foreach ($this->refs as $base) {
40 $path = new Path($base, $file);
41 if ($path->isFile()) {
42 return $path;
43 }
44 }
45 }
46
47 function glob(Path $path, $pattern, $flags = GLOB_BRACE) {
48 if (strlen($path->getBaseDir())) {
49 return glob($path->getFullPath($pattern), $flags);
50 }
51 $glob = array();
52 foreach ($this->refs as $ref) {
53 $glob = array_merge($glob, array_map(function ($fn) use ($ref) {
54 return substr($fn, strlen($ref));
55 }, glob($ref . $pattern, $flags)));
56 }
57 return $glob;
58 }
59 }