Repository navigation
Expand file tree
/
Copy pathController.php
More file actions
134 lines (114 loc) · 4.41 KB
/
Copy pathController.php
File metadata and controls
134 lines (114 loc) · 4.41 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
<?php
namespace Module\Generator;
use Stackbox;
use Alloy, Alloy\Request;
/**
* Generator/scaffold module
*/
class Controller extends Alloy\Module\ControllerAbstract
{
/**
* Ensure generator can only be invoked from command line
*/
public function init($action = null)
{
if(!\Kernel()->request()->isCli()) {
throw new Alloy\Exception\FileNotFound("Requested file or page not found. Please check the URL and try again.");
}
}
/**
* Scaffold module
*/
public function scaffoldAction(Request $request)
{
$kernel = \Kernel();
/**
* Variables we expect in CLI request:
*
* $name string Name of module
*/
$name = preg_replace('/[^a-zA-Z0-9_ ]/', '', $kernel->formatUnderscoreWord($request->name));
$name_table = preg_replace('/\s+/', '_', strtolower($name));
// URL-usable name
$name_url = preg_replace('/\s+/', '_', $name);
// Directory path
$name_dir = preg_replace('/\s+/', '/', $name);
// Valid PHP namespace
$namespace = preg_replace('/\s+/', '\\', $name);
// TODO: Make this dynamic and generated (allow user field definitions)
$fields = array(
'name' => array('type' => 'string', 'required' => true)
);
$field_string = "";
foreach($fields as $fieldName => $fieldInfo) {
// Flattens field definitions for writing in Entity.php file
// str_replace calls to remove some of the prettyprinting and odd formats var_export does by default
$field_string .= "'" . $fieldName . "' => " . str_replace(
array("array (", "\n", ",)"),
array("array(", "", ")"),
var_export($fieldInfo, true)
) . ",\n";
}
// Set tag variables
$generatorTagNames = compact('name', 'name_table', 'name_sanitized', 'name_url', 'name_dir', 'namespace', 'fields', 'field_string');
echo PHP_EOL;
// File paths
$scaffoldPath = __DIR__ . '/scaffold/';
$modulePath = $kernel->config('app.path.root') .'/Module/' . $name_dir . '/';
// Output
echo 'Generator Module Info' . PHP_EOL;
echo '-----------------------------------------------------------------------' . PHP_EOL;
echo 'Name = ' . $name . PHP_EOL;
echo 'Namespace = ' . $namespace . PHP_EOL;
echo 'Datasource (Table) = ' . $name_table . PHP_EOL;
echo 'Path = ' . $modulePath . PHP_EOL;
echo '-----------------------------------------------------------------------' . PHP_EOL;
echo PHP_EOL;
// Variables (in 'generator' namespace):
// * name
// * name_table
// * $fields
//
// Other variables
// * $fields (from Entity::fields())
// Build tag replecement set for scaffold-generated files
$generator_tag_start = '{$generator.';
$generator_tag_end = '}';
$generatorTags = array();
foreach($generatorTagNames as $tag => $val) {
if(is_array($val)) {
$val = var_export($val, true);
}
$generatorTags[$generator_tag_start . $tag . $generator_tag_end] = $val;
}
// Copy files and replace tokens
$scaffoldFiles = array(
'Controller.php',
'Entity.php',
'views/indexAction.html.php',
'views/newAction.html.php',
'views/editAction.html.php',
'views/deleteAction.html.php',
'views/viewAction.html.php'
);
foreach($scaffoldFiles as $sFile) {
$tmpl = file_get_contents($scaffoldPath . $sFile);
// Replace template tags
$tmpl = str_replace(array_keys($generatorTags), array_values($generatorTags), $tmpl);
// Ensure destination directory exists
$sfDir = dirname($modulePath . $sFile);
if(!is_dir($sfDir)) {
mkdir($sfDir, 0755, true);
}
// Write file to destination module directory
$result = file_put_contents($modulePath . $sFile, $tmpl);
if($result) {
echo "+ Generated '" . $sFile . "'";
} else {
echo "[ERROR] Unable to generate '" . $sFile . "'";
}
echo PHP_EOL;
}
echo PHP_EOL;
}
}