-
Notifications
You must be signed in to change notification settings - Fork 0
/
antonella
296 lines (247 loc) · 9.03 KB
/
antonella
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env php
<?php
class Antonella
{
public $dir=__DIR__;
public function process($data)
{
switch ($data[1])
{
case 'makeup':
return $this->makeup();
break;
case 'namespace':
return $this->newname($data);
break;
case 'make':
return $this->makeController($data);
break;
case 'widget':
return $this->MakeWidget($data);
default:
echo("Antonella no understand you. please read the manual in https://antonellaframework.com");
exit();
}
}
public function read_namespace()
{
$composer= file_get_contents($this->dir."/composer.json");
$composer_json=json_decode($composer);
$psr=$composer_json->autoload->{"psr-4"};
$namespace=substr(key($psr),0,-1);
return $namespace;
}
public function makeup()
{
echo("Antonella is packing the plugin \n");
$SO=strtoupper(substr(PHP_OS, 0, 3));
if($SO==='WIN')
{
$this->makeup_win();
}
else
{
$this->makeup_linux();
}
echo("The plugin's zip file is OK!");
}
public function makeup_win()
{
file_exists($this->dir.'/'.basename($this->dir).'.zip')?unlink($this->dir.'/'.basename($this->dir).'.zip'):false;
$zip = new ZipArchive();
$zip->open(basename($this->dir).'.zip', ZipArchive::CREATE);
$dirName =$this->dir;
if (!is_dir($dirName)) {
throw new Exception('Directory ' . $dirName . ' does not exist');
}
$dirName = realpath($dirName);
if (substr($dirName, -1) != '/') {
$dirName.= '/';
}
/*
* NOTE BY danbrown AT php DOT net: A good method of making
* portable code in this case would be usage of the PHP constant
* DIRECTORY_SEPARATOR in place of the '/' (forward slash) above.
*/
$dirStack = array($dirName);
//Find the index where the last dir starts
$cutFrom = strrpos(substr($dirName, 0, -1), '/')+strlen($this->dir)+1;
while (!empty($dirStack)) {
$currentDir = array_pop($dirStack);
$filesToAdd = array();
$dir = dir($currentDir);
while (false !== ($node = $dir->read())) {
if (($node == '..') || ($node == '.') || ($node=='.git') || ($node=='composer.json') || ($node=='.gitignore') || ($node =='antonella') || ($node=='readme.md') || ($node=='bitbucket-pipelines.yml') || ($node=='.gitmodules') ) {
continue;
}
if (is_dir($currentDir . $node)) {
//$currentDir=str_replace(__DIR__,"",$currentDir);
// echo($currentDir);
array_push($dirStack, $currentDir . $node . '/');
}
if (is_file($currentDir . $node)) {
$filesToAdd[] = $node;
}
}
$localDir = substr($currentDir, $cutFrom);
//echo($localDir)."\n";
$zip->addEmptyDir($localDir);
foreach ($filesToAdd as $file) {
$zip->addFile($currentDir . $file, $localDir . $file);
echo("Added $localDir$file into plugin \n");
}
}
$zip->close();
}
public function makeup_linux()
{
file_exists($this->dir.'/'.basename($this->dir).'.zip')?unlink($this->dir.'/'.basename($this->dir).'.zip'):false;
$zip = new ZipArchive();
$zip->open(basename($this->dir).'.zip', ZipArchive::CREATE);
$dirName = $this->dir;
if (!is_dir($dirName)) {
throw new Exception('Directory ' . $dirName . ' does not exist');
}
$dirName = realpath($dirName);
$filesToExclude = array('composer.json','.gitignore','antonella','readme.md', 'bitbucket-pipelines.yml', '.gitmodules');
$dirToExclude = array('.git');
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dirName),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach($files as $name => $file )
{
if (!$file->isDir() && !in_array($file->getFilename(), $filesToExclude)){
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($dirName) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
for($i=0;$i<$zip->numFiles;$i++){
$entry_info = $zip->statIndex($i);
foreach($dirToExclude as $dirExclude) {
$pos = strpos($entry_info["name"], $dirExclude);
if($pos !== false) {
$zip->deleteIndex($i);
}
}
}
$zip->close();
}
public function newname($data)
{
echo("Renaming the namespace... \n");
$slash=DIRECTORY_SEPARATOR;
$composer= file_get_contents($this->dir.$slash."composer.json");
$namespace=$this->read_namespace();
$core=file_get_contents($this->dir.$slash."antonella-framework.php");
$core=str_replace($namespace,$data[2],$core);
$composer=str_replace($namespace,$data[2],$composer);
file_put_contents($this->dir.$slash."antonella-framework.php",$core);
file_put_contents($this->dir.$slash."composer.json",$composer);
$dirName=$this->dir.$slash."src";
$dirName = realpath($dirName);
if (substr($dirName, -1) != '/') {
$dirName.= $slash;
}
$dirStack = array($dirName);
while (!empty($dirStack)) {
$currentDir = array_pop($dirStack);
$filesToAdd = array();
$dir = dir($currentDir);
while (false !== ($node = $dir->read())) {
if (($node == '..') || ($node == '.'))
{
continue;
}
if (is_dir($currentDir . $node)) {
array_push($dirStack, $currentDir . $node . $slash);
}
if (is_file($currentDir . $node)) {
//echo($currentDir."$node \n");
$file=file_get_contents($currentDir.$node);
$file= str_replace($namespace,$data[2],$file);
file_put_contents($currentDir.$node,$file);
// echo($file." \n");
}
}
}
exec("composer dump-autoload");
exit ("The new namespace is $data[2] ");
}
public function makecontroller($data)
{
$namespace=$this->read_namespace();
$input=
"<?php
namespace $namespace;
class $data[2]
{
public function __construct()
{
}
}";
file_put_contents(__DIR__."/src/$data[2].php", $input);
exit("Controller $data[2].php created info src folder");
}
public function MakeWidget($data)
{
$namespace=$this->read_namespace();
$input=
"<?php
namespace $namespace;
class $data[2] extends \WP_Widget
{
/**
* Please complete the public variables
*/
public \$name_widget=''; // <--complete this
public \$options=
[
'classname'=>'', // <-- complete this
'description'=>'' // <-- complete this
];
public \$form_values=
[
//Example: 'title'=>'the best plugin', 'url'=>'https://antonellaframework.com'
];
public function __construct()
{
\$this->WP_Widget('$data[2]', \$this->name_widget, \$this->options);
}
function form(\$instance) {
// Build the Admin's Widget form
\$instance = wp_parse_args((array)\$instance, \$this->form_values);
\$html=\"\";
foreach (\$instance as \$key=>\$inst)
{
\$html.=\"<p>\$key<input class='widefat' type='text' name='{\$this->get_field_name(\$key)}' value='\".esc_attr(\$inst).\"'/></p>\";
}
echo \$html;
}
function update(\$new_instance, \$old_instance) {
// Save the Widget Options
\$instances = \$old_instance;
foreach(\$new_instance as \$key => \$value)
{
\$instances[\$key]= sanitize_text_field(\$new_instance[\$key]);
}
return \$instances;
}
function widget(\$args, \$instance) {
//Build the code for show the widget in plubic zone.
extract(\$args);
\$html=\"\";
// you can edit this function for make the html//
//
////////////////////////////////////////////////
echo \$html;
}
}";
file_put_contents(__DIR__."/src/$data[2].php", $input);
exit("The Widget $data[2].php created info src folder");
}
}
$antonella=new Antonella();
exit($antonella->process($argv));