-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathPath.php
More file actions
131 lines (113 loc) · 3.6 KB
/
Path.php
File metadata and controls
131 lines (113 loc) · 3.6 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of the Phalcon Developer Tools.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Phalcon\DevTools\Builder;
use Phalcon\Config\Config;
use Phalcon\Config\Adapter\Ini as ConfigIni;
use Phalcon\DevTools\Builder\Exception\BuilderException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class Path
{
/**
* @var string|null
*/
protected $rootPath = null;
public function __construct(string $rootPath = null)
{
$this->rootPath = $rootPath ?: realpath('.') . DIRECTORY_SEPARATOR;
}
/**
* Tries to find the current configuration in the application
*
* @param string $type Config type: ini | php
* @return Config
* @throws BuilderException
*/
public function getConfig($type = null): Config
{
$types = ['php' => true, 'ini' => true];
$type = isset($types[$type]) ? $type : 'ini';
foreach (['app/config/', 'config/', 'apps/config/', 'apps/frontend/config/'] as $configPath) {
if ('ini' === $type && file_exists($this->rootPath . $configPath . 'config.ini')) {
return new ConfigIni($this->rootPath . $configPath . 'config.ini');
}
if (file_exists($this->rootPath . $configPath. 'config.php')) {
$config = include($this->rootPath . $configPath . 'config.php');
if (is_array($config)) {
$config = new Config($config);
}
return $config;
}
}
$directory = new RecursiveDirectoryIterator('.');
$iterator = new RecursiveIteratorIterator($directory);
foreach ($iterator as $f) {
if (false !== strpos($f->getPathName(), 'config.php')) {
$config = include $f->getPathName();
if (is_array($config)) {
$config = new Config($config);
}
return $config;
}
if (false !== strpos($f->getPathName(), 'config.ini')) {
return new ConfigIni($f->getPathName());
}
}
throw new BuilderException("Builder can't locate the configuration file");
}
/**
* @param null|string $path
*/
public function setRootPath(?string $path = null)
{
$this->rootPath = rtrim(str_replace('/', DIRECTORY_SEPARATOR, $path), '\\/') . DIRECTORY_SEPARATOR;
return $this;
}
/**
* @param null|string $path
*/
public function getRootPath(?string $path = null): string
{
return $this->rootPath . ($path ? trim($path, '\\/') . DIRECTORY_SEPARATOR : '');
}
public function appendRootPath($pathPath): void
{
$this->setRootPath($this->getRootPath() . rtrim($pathPath, '\\/') . DIRECTORY_SEPARATOR);
}
/**
* Check if a path is absolute
*
* @param string $path Path to check
* @return bool
*/
public function isAbsolutePath(string $path): bool
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
if (preg_match('/^[A-Z]:\\\\/', $path)) {
return true;
}
} else {
if (substr($path, 0, 1) == DIRECTORY_SEPARATOR) {
return true;
}
}
return false;
}
/**
* Check Phalcon system dir
*
* @return bool
*/
public function hasPhalconDir(): bool
{
return file_exists($this->rootPath . '.phalcon');
}
}