forked from Fabsolute/Phalcon-API-Helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-api
executable file
·66 lines (53 loc) · 1.92 KB
/
simple-api
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
#!/usr/bin/env php
<?php
define('TARGET_PATH', getcwd());
define('CURRENT_PATH', __DIR__ . '/bundle/');
function makeDir($dir_name)
{
if (!file_exists($dir_name)) {
mkdir($dir_name);
}
}
function clearVariables($subject, $folder_name, $namespace_name)
{
$subject = str_replace('{{NamespaceName}}', $namespace_name, $subject);
return str_replace('{{FolderName}}', $folder_name, $subject);
}
if ($argc === 4 && $argv[1] === 'new') {
$folder_name = $argv[2];
$namespace_name = $argv[3];
makeDir(TARGET_PATH . '/' . $folder_name);
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(CURRENT_PATH),
RecursiveIteratorIterator::SELF_FIRST
);
$folder_list = [];
$file_list = [];
foreach ($iterator as $file) {
if ($file->getFilename() !== '..' && $file->getFilename() !== '.') {
$file_name = $file->getRealPath();
$file_name = str_replace(CURRENT_PATH, '', $file_name);
if ($file->isDir()) {
$folder_list[] = $file_name;
} else {
if (substr($file_name, -5) === '.fabs') {
$file_list[] = $file_name;
}
}
}
}
$folder_list = array_values(array_unique($folder_list));
$file_list = array_values(array_unique($file_list));
foreach ($folder_list as $folder) {
makeDir(TARGET_PATH . '/' . $folder_name . '/' . $folder);
}
foreach ($file_list as $file_name) {
$file_content = clearVariables(file_get_contents(CURRENT_PATH . '/' . $file_name), $folder_name, $namespace_name);
$new_file_name = str_replace('.fabs', '', clearVariables($file_name, $folder_name, $namespace_name));
file_put_contents(TARGET_PATH . '/' . $folder_name . '/' . $new_file_name, $file_content);
}
echo 'project created';
} else {
echo 'usage: simple-api new folder_name NamespaceName';
}
exit;