1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Console; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Helper\FileSystem\Directory; |
6
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
7
|
|
|
use Ffcms\Core\Helper\Type\Str; |
8
|
|
|
|
9
|
|
|
class ArchBuilder |
10
|
|
|
{ |
11
|
|
|
public $message; |
12
|
|
|
|
13
|
|
|
public function createObject($name, $type) |
14
|
|
|
{ |
15
|
|
|
$singleName = false; |
16
|
|
|
if (!Str::contains('/', $name)) { |
17
|
|
|
if ($type === 'ActiveRecord') { |
18
|
|
|
$singleName = true; |
19
|
|
|
} else { |
20
|
|
|
$this->message = 'Command dosn\'t contains valid name. Example: Front/SomeForm, Admin/SomePkg/SomeInput'; |
21
|
|
|
return false; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$objectDirPath = null; |
|
|
|
|
26
|
|
|
$objectNamespace = null; |
|
|
|
|
27
|
|
|
$objectName = null; |
|
|
|
|
28
|
|
|
|
29
|
|
|
$objectTemplate = null; |
30
|
|
|
|
31
|
|
|
if ($singleName) { |
32
|
|
|
$objectDirPath = root . '/Apps/' . $type . '/'; |
33
|
|
|
$objectNamespace = 'Apps\\' . $type; |
34
|
|
|
$objectName = ucfirst($name); |
35
|
|
|
} else { |
36
|
|
|
$split = explode('/', $name); |
37
|
|
|
$workground = ucfirst(strtolower(array_shift($split))); |
38
|
|
|
$objectName = ucfirst(array_pop($split)); |
39
|
|
|
|
40
|
|
|
$subName = false; |
41
|
|
|
if (count($split) > 0) { // some sub-namespace / folder path |
42
|
|
|
foreach ($split as $part) { |
43
|
|
|
if (Str::length($part) > 0) { |
44
|
|
|
$subName[] = ucfirst(strtolower($part)); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($type === 'Widget') { |
50
|
|
|
$objectDirPath = root . '/Widgets/' . $workground; |
51
|
|
|
$objectNamespace = 'Widgets\\' . $workground; |
52
|
|
|
} else { |
53
|
|
|
$objectDirPath = root . '/Apps/' . $type . '/' . $workground; |
54
|
|
|
$objectNamespace = 'Apps\\' . $type . '\\' . $workground; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (false !== $subName) { |
58
|
|
|
$objectDirPath .= '/' . implode('/', $subName); |
59
|
|
|
$objectNamespace .= '\\' . implode('\\', $subName); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
// try to find workground-based controller |
64
|
|
|
if (File::exist('/Private/Carcase/' . $workground . '/' . $type . '.tphp')) { |
65
|
|
|
$objectTemplate = File::read('/Private/Carcase/' . $workground . '/' . $type . '.tphp'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!Directory::exist($objectDirPath) && !Directory::create($objectDirPath)) { |
70
|
|
|
$this->message = 'Directory could not be created: ' . $objectDirPath; |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($objectTemplate === null) { |
75
|
|
|
$objectTemplate = File::read('/Private/Carcase/' . $type . '.tphp'); |
76
|
|
|
if (false === $objectTemplate) { |
77
|
|
|
$this->message = 'Php template file is not founded: /Private/Carcase/' . $type . '.tphp'; |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$objectContent = Str::replace(['%namespace%', '%name%'], [$objectNamespace, $objectName], $objectTemplate); |
|
|
|
|
83
|
|
|
$objectFullPath = $objectDirPath . '/' . $objectName . '.php'; |
84
|
|
|
if (File::exist($objectFullPath)) { |
85
|
|
|
$this->message = $type . ' is always exist: ' . $objectFullPath; |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
File::write($objectFullPath, $objectContent); |
89
|
|
|
$this->message = $type . ' template was created: [' . $objectName . '] in path: ' . Str::replace(root, '', $objectDirPath); |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.