-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathAbstractComponent.php
More file actions
147 lines (129 loc) · 3.27 KB
/
Copy pathAbstractComponent.php
File metadata and controls
147 lines (129 loc) · 3.27 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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\Component;
use Phalcon\Config\Config;
use Phalcon\DevTools\Builder\Exception\BuilderException;
use Phalcon\DevTools\Builder\Path;
use Phalcon\DevTools\Script\Color;
use Phalcon\DevTools\Validation\Validator\Namespaces;
use Phalcon\Validation;
/**
* Base class for builder components
*/
abstract class AbstractComponent
{
/**
* Builder Options
*
* @var Config
*/
protected $options = null;
/**
* Path Component
*
* @var Path
*/
protected $path;
/**
* Create Builder object
*
* @param array $options Builder options
*/
public function __construct(array $options = [])
{
$this->options = new Config($options);
$this->path = new Path(realpath('.') . DIRECTORY_SEPARATOR);
}
/**
* @param string $namespace
* @return bool
* @throws BuilderException
*/
protected function checkNamespace(string $namespace): bool
{
$validation = new Validation();
$validation->add('namespace', new Namespaces([
'allowEmpty' => true,
]));
$messages = $validation->validate(['namespace' => $namespace]);
if (count($messages) > 0) {
$errors = [];
foreach ($messages as $message) {
$errors[] = $message->getMessage();
}
throw new BuilderException(sprintf('%s', implode(PHP_EOL, $errors)));
}
return true;
}
/**
* Tries to find the current configuration in the application
*
* @param string $type Config type: ini | php
* @return Config
* @throws BuilderException
*/
protected function getConfig($type = null): Config
{
return $this->path->getConfig($type);
}
/**
* Check if a path is absolute
*
* @param string $path Path to check
* @return bool
*/
public function isAbsolutePath(string $path): bool
{
return $this->path->isAbsolutePath($path);
}
/**
* Check if the script is running on Console mode
*
* @return bool
*/
public function isConsole(): bool
{
return PHP_SAPI == 'cli';
}
/**
* Check if the current adapter is supported by Phalcon
*
* @param string $adapter
* @return bool
* @throws BuilderException
*/
public function isSupportedAdapter(string $adapter): bool
{
if (!class_exists('\Phalcon\Db\Adapter\Pdo\\' . $adapter)) {
throw new BuilderException("Adapter $adapter is not supported");
}
return true;
}
/**
* Shows a success notification
*
* @param string $message
*/
protected function notifySuccess(string $message): void
{
print Color::success($message);
}
/**
* Shows a info notification
*
* @param string $message
*/
protected function notifyInfo(string $message): void
{
print Color::info($message);
}
abstract public function build();
}