-
Notifications
You must be signed in to change notification settings - Fork 420
/
create_example.php
executable file
·171 lines (150 loc) · 4.12 KB
/
create_example.php
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env php
<?php
/**
* A commandline script to create an example and the needed files:
*
* $ bin/create_example.php my_new_example
*
* ... and the folder my_new_example will be created in the examples/ folder containing 3 files:
*
* my_new_example/my_new_example.mustache
* my_new_example/my_new_example.txt
* my_new_example/MyNewExample.php
*/
// some constants
define('USAGE', <<<USAGE
USAGE: {$argv[0]} example_name
This creates a new example and the corresponding files in the examples/ directory
USAGE
);
define('EXAMPLE_PATH', realpath(dirname(__FILE__) . '/../test/fixtures/examples'));
/**
* transform a string to lowercase using underlines.
* Examples:
* String -> string
* AString -> a_string
* SomeStrings -> some_strings
* AStringMore -> a_string_more
*
* @param string $name
* @access public
* @return string
*/
function getLowerCaseName($name) {
return preg_replace_callback("/([A-Z])/", create_function (
'$match',
'return "_" . strtolower($match[1]);'
), lcfirst($name));
}
/**
* transform a string to Uppercase (camelcase)
* Examples
* string -> String
* a_string -> AString
* some_strings -> SomeStrings
* a_string_more -> AStringMore -> a_string_more
*
* @param string $name
* @access public
* @return string
*/
function getUpperCaseName($name) {
return preg_replace_callback("/_([a-z])/", create_function (
'$match',
'return strtoupper($match{1});'
), ucfirst($name));
}
/**
* return the given value and echo it out appending "\n"
*
* @param mixed $value
* @access public
* @return mixed
*/
function out($value) {
echo $value . "\n";
return $value;
}
/**
* create Path for certain files in an example
* returns the directory name if only $directory is given.
* if an extension is given a complete filename is returned.
* the returned filename will be echoed out
*
* @param string $directory directory without / at the end
* @param string $filename filename without path and extension
* @param string $extension extension of the file without "."
* @access public
* @return string
*/
function buildPath($directory, $filename = null, $extension = null) {
return out(EXAMPLE_PATH . '/' . $directory.
($extension !== null && $filename !== null ? '/' . $filename. "." . $extension : ""));
}
/**
* creates the directory for the example
* the script die()'s if mkdir() fails
*
* @param string $directory
* @access public
* @return void
*/
function createDirectory($directory) {
if(!@mkdir(buildPath($directory))) {
die("FAILED to create directory\n");
}
}
/**
* create a file for the example with the given $content
* the script die()'s if fopen() fails
*
* @param string $directory directory without / at the end
* @param string $filename filename without path and extension
* @param string $extension extension of the file without "."
* @param string $content the content of the file
* @access public
* @return void
*/
function createFile($directory, $filename, $extension, $content = "") {
$handle = @fopen(buildPath($directory, $filename, $extension), "w");
if($handle) {
fwrite($handle, $content);
fclose($handle);
} else {
die("FAILED to create file\n");
}
}
/**
* routine to create the example directory and 3 files
*
* if the $example_name is "SomeThing" the following files will be created
* examples/some_thing
* examples/some_thing/some_thing.mustache
* examples/some_thing/some_thing.txt
* examples/some_thing/SomeThing.php
*
* @param mixed $example_name
* @access public
* @return void
*/
function main($example_name) {
$lowercase = getLowerCaseName($example_name);
$uppercase = getUpperCaseName($example_name);
createDirectory($lowercase);
createFile($lowercase, $lowercase, "mustache");
createFile($lowercase, $lowercase, "txt");
createFile($lowercase, $uppercase, "php", <<<CONTENT
<?php
class {$uppercase} {
}
CONTENT
);
}
// check if enougth arguments are given
if(count($argv) > 1) {
// get the name of the example
$example_name = $argv[1];
main($example_name);
} else {
echo USAGE;
}