1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Console; |
4
|
|
|
|
5
|
|
|
use Yarak\Config\Config; |
6
|
|
|
use Yarak\Helpers\Filesystem; |
7
|
|
|
use Yarak\Console\Output\Output; |
8
|
|
|
use Yarak\Exceptions\WriteError; |
9
|
|
|
|
10
|
|
|
class CommandCreator |
11
|
|
|
{ |
12
|
|
|
use Filesystem; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Yarak config. |
16
|
|
|
* |
17
|
|
|
* @var Config |
18
|
|
|
*/ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Output strategy. |
23
|
|
|
* |
24
|
|
|
* @var Output |
25
|
|
|
*/ |
26
|
|
|
protected $output; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Construct. |
30
|
|
|
* |
31
|
|
|
* @param Config $config |
32
|
|
|
* @param Output $output |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Config $config, Output $output) |
35
|
|
|
{ |
36
|
|
|
$this->config = $config; |
37
|
|
|
$this->output = $output; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new command with given name. |
42
|
|
|
* |
43
|
|
|
* @param string $name |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function create($name) |
48
|
|
|
{ |
49
|
|
|
if (class_exists($name)) { |
50
|
|
|
throw WriteError::classExists($name); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$commandsDir = $this->config->getCommandsDirectory(); |
54
|
|
|
|
55
|
|
|
$this->makeDirectoryStructure([$commandsDir]); |
56
|
|
|
|
57
|
|
|
$this->writeFile( |
58
|
|
|
$path = $commandsDir.$name.'.php', |
59
|
|
|
$this->getStub($name) |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$this->output->writeInfo("Successfully created command {$name}."); |
63
|
|
|
|
64
|
|
|
return $path; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the stub file and insert name. |
69
|
|
|
* |
70
|
|
|
* @param string $name |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
protected function getStub($name) |
75
|
|
|
{ |
76
|
|
|
$stub = file_get_contents(__DIR__.'/Stubs/command.stub'); |
77
|
|
|
|
78
|
|
|
$stub = str_replace('CLASSNAME', $name, $stub); |
79
|
|
|
|
80
|
|
|
return $this->setNamespace($stub, $this->resolveNamespace()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Resolve the command namespace. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
protected function resolveNamespace() |
89
|
|
|
{ |
90
|
|
|
if ($this->config->has(['namespaces', 'commandsNamespace'])) { |
91
|
|
|
return $this->config->get(['namespaces', 'commandsNamespace']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->guessNamespace($this->config->getCommandsDirectory()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the namespace in the command stub. |
99
|
|
|
* |
100
|
|
|
* @param string $stub |
101
|
|
|
* @param string $namespace |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function setNamespace($stub, $namespace = null) |
106
|
|
|
{ |
107
|
|
|
if ($namespace) { |
|
|
|
|
108
|
|
|
return str_replace( |
109
|
|
|
'NAMESPACE', |
110
|
|
|
"\nnamespace {$namespace};\n", |
111
|
|
|
$stub |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return str_replace('NAMESPACE', '', $stub); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: