This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.sh
executable file
·151 lines (128 loc) · 4.31 KB
/
controller.sh
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
148
149
150
151
#!/bin/bash
#script_dirname=`dirname $0`;
# get where the core directory resides
core_path="$( readlink -f "$( dirname "$0" )/" )";
core_parent_path="$( readlink -f "$( dirname "$0" )/../" )";
uri='';
path='app';
help=0;
for p in $*;
do
[[ $p =~ ^--path=(.*) ]] && path="${BASH_REMATCH[1]}";
[[ $p =~ ^/(.*) ]] && uri=$p;
[[ $p =~ ^--help ]] && help=1;
done;
if [ $help -eq 1 ]
then
cat <<EOF
Generate Controller:
core/controller.sh [--path=<application path>] [uri]
Example
core/controller.sh /products/details
core/controller.sh --path='/var/sites/app' /products/lists
EOF
exit;
fi
if [ -z $uri ]
then
echo "$uri is required and must start with a /"
exit
fi
path="$( readlink -f "$( basename "$path" )" )";
pathname=`basename $path`;
controller_path="$path/controllers";
# array uri
classname=(`echo $uri | tr '[A-Z]' '[a-z]' | sed -e 's/\/./\U&\E/g' | tr "/" " " `)
classname=$(printf "%s" "${classname[@]}")
arrayuri=(`echo $uri | tr '[A-Z]' '[a-z]' | tr "/" " "`)
filename=$(printf "_%s" "${arrayuri[@]}")
filenamepath="$controller_path/${filename:1}.php"
if [ -f $filenamepath ]
then
echo -e "\n* $filenamepath already exists\n"
exit
fi
cat > $filenamepath <<EOF
<?php
namespace Core\App\Controllers;
use Core\Controller;
use Core\Exception;
use Core\App\Module;
use Core\Tools;
class $classname extends Controller
{
/**
* assign a variable to template
* render('foo', 'bar');
* \$render = render();
* \$render['foo'] = 'bar';
*
* assign view function
* render('foo()', function(){});
* assign view filter
* render('foo|', function(){});
* assign test filter
* render('foo?', function(){});
*
* getting post/get variable
* params()->post['foo'];
* params()->get['foo'];
*
* get, assign cookie variable
* params()->cookie['foo'];
* params()->cookie['foo'] = 'bar';
* params()->cookie['foo'] = null; // expire cookie
* params()->cookies(array('expire' => '+1 day', 'path' => '/')); // setup cookie settings
*
* session start
* params()->session(); // start session
* params()->session('mysession'); // start named session
*
* session vars
* echo params()->session['foo'];
* params()->session['foo'] = 'bar'; // assign a session var 'foo' with value 'bar'
* params()->flash['foo'] = 'bar'; // assign a flash session var 'foo' with value 'bar'
*
* render template
* render()->template = 'template'; // assign template
* render()->default = 'default_template'; // assign default template
* render content
* render()->inline = 'hello world'; // output string, lambda function can also be passed
* render()->json = array('[status]', '[result]', '[notice]', '[response]'); // output json, lambda function can also be passed
* render()->file = 'path to file';// raw content a.k.a. file, lambda function can also be passed
* render()->content_type = 'mime here'; // default is text/html
* render()->nocache = true;
* render()->attachment = 'filnamehere.txt';
* render()->last_modified = <timestamp>; // send if modified header
* render()->status = 200;
*
* params validation, sanitization
* params(\$array)->validate(function(\$validate){});
* \$get_foo = params('get.foo')->sanitize(function(\$sanitize){});
*
* redirects and http status:
* redirect('path');
* redirect('path', true); // sends 301 Moved Permanently
* redirect('path', array('foo' => 'bar'), false); // redirect with get parameters
*
*/
protected function __construct()
{
}
protected function __destruct()
{
}
public function index()
{
// static \$greedy; // method is greedy
// static \$method; // if function returned is controller, this variable will route to a method, by default it's index
// static \$reserved; // public method should not be directly available through request
// static \$template; // assign default template without calling render()->template
// static \$constraints; // an array of regular expression string that would be check against parameters passed
}
}
EOF
cat <<EOF
Controller Class $classname Created
In $filenamepath
EOF