This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathccg.php
547 lines (480 loc) · 13.1 KB
/
ccg.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?php
/**
* Code Completion Generator
* By EthaiZone.com <[email protected]>
* Code task from http://forums.laravel.io/viewtopic.php?id=742&p=2
* Thanks - https://github.com/danielboendergaard/laravel-helpers/blob/master/ide_helper.php
*/
class Ccg_Task extends Task
{
/**
* Header strings to add with head of generate code.
* @var type array
*/
private static $header = array(
'<?php',
'
/**
* Code completion code.
* Generated by ccg Artisan command.
*/
/**
* Short-cut for constructor method chaining.
*
* @param mixed $object
* @return object
*/
function with($object)
{
return $object;
}
class Asset_Container extends Laravel\Asset_Container{};
class Query extends Laravel\Database\Query{};
',
);
/**
* Footer string to add with tail of generate code.
* @var type array
*/
private static $footer = array(
'// End of code generation'
);
/**
* Abstract classes.
* @var type array
*/
private static $abstracts = array( 'Authenticator' );
/**
* Extra codes to add as function definitions.
*
* When not enough to complition on specific classes,
* you can add any function want to complite.
*
* @var type array
*/
private static $extra = array(
'DB' => array(
'
//From /laravel/database/connection.php
/**
* Execute a callback wrapped in a database transaction.
*
* @param callback $callback
* @return void
*/
public function transaction($callback) {}
/**
* Execute a SQL query and return an array of StdClass objects.
*
* @param string $sql
* @param array $bindings
* @return array
*/
public function query($sql, $bindings = array()) {}
',
),
'Eloquent' => array(
'
//Dummy variable - common use
/**
* Timestamp of record when created.
*
* @var string
*/
public $created_at;
/**
* Timestamp of record when updated.
*
* @var string
*/
public $updated_at;
//Dummy method - I can\'t find real method.
/**
* Eager load table that you want to join
*
* <code>
* Book::with("author")->get();
* Book::with(array("author", "author.contacts"))->get();
* User::with(array("posts" => function($query)
* {
* $query->where("title", "like", "%first%");
* }))->get();
* </code>
*
* @param mixed $column
* @return Query
*/
public static function with($column) {}
/**
* Return total records that you want query
*
* @return int
*/
public static function count() {}
//From /laravel/database/query.php
/**
* Add a where condition to the query.
*
* @param string $column
* @param string $operator
* @param mixed $value
* @param string $connector
* @return Query
*/
public function where($column, $operator = null, $value = null, $connector = \'AND\') {}
/**
* Add a where in condition to the query.
*
* @param string $column
* @param array $values
* @param string $connector
* @param bool $not
* @return Query
*/
public function where_in($column, $values, $connector = \'AND\', $not = false) {}
/**
* Add an ordering to the query.
*
* @param string $column
* @param string $direction
* @return Query
*/
public function order_by($column, $direction = \'asc\') {}
/**
* Set the query limit.
*
* @param int $value
* @return Query
*/
public function take($value) {}
/**
* Find a record by the primary key.
*
* @param int $id
* @param array $columns
* @return Query
*/
public function find($id, $columns = array(\'*\')) {}
/**
* Add an array of columns to the SELECT clause.
*
* @param array $columns
* @return Query
*/
public function select($columns = array(\'*\')) {}
/**
* Get an array with the values of a given column.
*
* @param string $column
* @param string $key
* @return array
*/
public function lists($column, $key = null) {}
/**
* Add a join clause to the query.
*
* @param string $table
* @param string $column1
* @param string $operator
* @param string $column2
* @param string $type
* @return Query
*/
public function join($table, $column1, $operator = null, $column2 = null, $type = \'INNER\') {}
',
),
'Log' => array(
'
//Common used.
/**
* Write an "error" message to the log file
*
* @param string $message
* @return void
*/
public static function error($message) {}
/**
* Write an "warning" message to the log file
*
* @param string $message
* @return void
*/
public static function warning($message) {}
/**
* Write an "info" message to the log file
*
* @param string $message
* @return void
*/
public static function info($message) {}
',
),
//Navbar from Bootstrapper Bundle
'Navbar' => array(
'
/**
* Create a new Navbar instance with dark color.
*
* @param array $attributes An array of attributes for the current navbar
* @param const $type The type of Navbar to create
*
* @return Navbar
*/
function inverse($attributes = array(), $type = Navbar::STATIC_BAR) {}
',
),
);
/**
* Display C.C. code
*
* Usage:
* php artisan ccg
*
* @return integer Execution result code
*/
public function run()
{
echo $this->generator();
exit(0);
}
/**
* Generate C.C. code and save to file as php.
*
* Usage:
* // save to application/ccc.php
* php artisan ccg:save
*
* // save file specified by parameter
* php artisan ccg:save application/cc_helper.php
*
* @param string $outfile Output file name
* @return integer Execution result code
*/
public function save($arguments)
{
$outfile = count($arguments) == 0 ? $outfile = path('app')."ccg.php" : $arguments[0];
$codes = $this->generator();
File::put($outfile, $codes);
exit(0);
}
/**
* Code generator
*
* @return string Generated code
*/
private function generator()
{
$codeline = array( );
$codeline[] = self::$header;
$aliases = Config::get('application.aliases');
foreach ( $aliases as $alias => $namespace ) {
switch ( $alias ) {
case 'Auth' :
switch ( Config::get('auth.driver') ) {
case 'eloquent' :
$codeline[] = $this->format_code($alias, 'Laravel\Auth\Drivers\Eloquent');
break;
case 'fluent' :
$codeline[] = $this->format_code($alias, 'Laravel\Auth\Drivers\Fluent');
break;
default :
// throw new \Exception("Bad Auth driver [$alias] specified.");
$codeline[] = "// Auth driver [$alias] is not supported by this generator. Please set this by your hand.";
$codeline[] = '// class '.$alias.' extends '.$namespace.'{};';
}
break;
/*
case 'DB' :
switch ( Config::get('database.default') ) {
case 'sqlite':
$namespace = 'Laravel\Database\Query\Grammars\SQLite';
break;
case 'mysql':
$namespace = 'Laravel\Database\Query\Grammars\MySQL';
break;
case 'pgsql':
$namespace = 'Laravel\Database\Query\Grammars\Postgres';
break;
case 'sqlsrv':
$namespace = 'Laravel\Database\Query\Grammars\SQLServer';
break;
default:
// throw new \Exception("Bad database default driver [$alias] specified.");
$codeline[] = "// DB default driver [$alias] is not supported by this generator. Please set this by your hand.";
$codeline[] = '// class '.$alias.' extends '.$namespace.'{};';
}
$codeline[] = $this->format_code($alias, $namespace);
break;
*
*/
case 'Cache' :
switch ( Config::get('cache.driver') ) {
case 'apc':
$codeline[] = $this->format_code($alias, 'Laravel\Cache\Drivers\APC');
break;
case 'file':
$codeline[] = $this->format_code($alias, 'Laravel\Cache\Drivers\File');
break;
case 'memcached':
$codeline[] = $this->format_code($alias, 'Laravel\Cache\Drivers\Memcached');
break;
case 'memory':
$codeline[] = $this->format_code($alias, 'Laravel\Cache\Drivers\Memory');
break;
case 'redis':
$codeline[] = $this->format_code($alias, 'Laravel\Cache\Drivers\Redis');
break;
case 'database':
$codeline[] = $this->format_code($alias, 'Laravel\Cache\Drivers\Database');
break;
default:
// throw new \Exception("Bad Cache driver [$alias] specified.");
$codeline[] = "// Cache driver [$alias] is not supported by this generator. Please set this by your hand.";
$codeline[] = '// class '.$alias.' extends '.$namespace.'{};';
}
break;
case 'Session' :
$add = $this->get_method_data('session/payload.php', 'Payload');
$codeline[] = $this->format_code($alias, "Laravel\Session", $add);
break;
/*
case 'Asset' :
$add = $this->get_method_data('asset.php', 'Asset_Container');
$codeline[] = $this->format_code($alias, "Laravel\Asset", $add);
break;
*/
/*
case 'Eloquent' :
$add = $this->get_method_data('database/query.php', 'Query');
$codeline[] = $this->format_code($alias, "Laravel\Database\Eloquent\Model", $add);
break;*/
default :
$codeline[] = $this->format_code($alias, $namespace);
}
}
$codeline[] = self::$footer;
$codeline[] = '';
$flatcode = array( );
array_walk_recursive($codeline, function($val) use (&$flatcode) {
$flatcode[] = $val;
});
return implode(PHP_EOL, $flatcode);
}
private function get_method_data($filename, $class)
{
$file = path('sys').$filename;
if(! file_exists($file)) return array("// Failed to read driver soruce file:[/laravel/$filename]");
$script = trim(strtr(File::get($file), '\\', '/'));
preg_match('#class '.$class.' \{(.+)\}#s', $script, $match);
if(empty($match[1])) return array("// Failed to read driver soruce file:[/laravel/$filename]");
$script = $match[1];
$regex_replace = array(
array('#/\*.+?@var.+?\$.+?;#s', ''),
array('#\{[^\{\}]+\}#s', ''),
array('#\{[^\{\}]+\}#s', ''),
array('#\{[^\{\}]+\}#s', ''),
array('#\{[^\{\}]+\}#s', ''),
array('#(function.+\))\n#', "\\1 {}\n"),
array('#public function __construct[^\n]*\([^\n]*\) \{\}#', ''),
array('#protected (static )?function[^\n]*\([^\n]*\) \{\}#', ''),
array('#private (static )?function[^\n]*\([^\n]*\) \{\}#', '')
//array('#/\*\*[^/]+\*/[\s]+protected (static )?function[^\n]+\([^\n]*\) \{\}#s', ''),
//array('#/\*\*[^/]+\*/[\s]+private (static )?function[^\n]+\([^\n]*\) \{\}#s', ''),
//array('#/\*\*[^/]+\*/[\s]+public function __construct[^\n]*\([^\n]*\) \{\}#s', ''),
//array('#\n[\s]+?\n#s', "\n"),
);
foreach ($regex_replace as $regex)
{
$script = preg_replace($regex[0], $regex[1], $script);
}
//Delete old phpdoc that method deleted
//I must delete by pointer not regex because I tested many regex to do it but result isn't good.
$script = trim($script);
$delete_list = array();
$offset = 0;
while($offset !== FALSE)
{
$start = strpos($script, "/**", $offset);
//var_dump($start);die();
if($start !== FALSE)
{
$stop = strpos($script, "*/", $start+3);
//var_dump($stop);
if($stop !== FALSE)
{
$next = strpos($script, "/**", $stop+2);
//var_dump($next);
//echo "\n";
if($next !== FALSE)
{
$between_phpdocs = substr($script, $stop+2, $next-$stop-3);
$look = strpbrk($between_phpdocs, implode('', range('a', 'z')));
//var_dump($between_phpdocs );die();
if($look === FALSE)
{
//delete old phpdoc
$delete_list[] = array($start, ($stop-$start)+2);
$offset = $stop+2;
//echo 'start:'.$start.' - stop:'.$stop.' - next:'.$next."\n";
} else {
$offset = $next-3;
}
} else {
$between_phpdocs = substr($script, $stop, strlen($script)-$stop);
$look = strpbrk($between_phpdocs, implode('', range('a', 'z')));
//Catch last old phpdoc/func
if($look === FALSE)
{
$delete_list[] = array($start, ($stop-$start)+2);
//echo 'start:'.$start.' - stop:'.$stop.' - next:'.$next."\n";
}
$offset = FALSE;
}
} else {
$offset = FALSE;
}
} else {
$offset = FALSE;
}
}
//Delete old phpdoc - reverse order.
//var_dump($delete_list);die();
$delete_list = array_reverse($delete_list);
foreach ($delete_list as $k => $delete) {
//File::put($k.'.txt', substr($script, $delete[0], $delete[1]));
$script = substr_replace($script, '', $delete[0], $delete[1]);
}
//Delete blank line.
$script = preg_replace('#\n[\s]+\n#s', "\n\n", $script);
return "\t//Methods from /laravel/$filename - Class $class\n\n\t".trim($script);
}
/**
* Format class extends
*
* @param string $alias Alias name
* @param string $namespace Namespace
* @return array
*/
private function format_code($alias, $namespace, $addcodes = array( ))
{
$abs = in_array($alias, self::$abstracts) ? 'abstract ' : '';
if ( key_exists($alias, self::$extra) || !empty($addcodes) ) {
$ret = array( );
$ret[] = $abs.'class '.$alias.' extends '.$namespace.'{';
if ( key_exists($alias, self::$extra) )
$ret[] = self::$extra[$alias];
if ( !empty($addcodes) )
{
if(!is_array($addcodes)) $addcodes = explode(PHP_EOL, $addcodes);
$ret[] = $addcodes;
}
$ret[] = '};';
return $ret;
}
else {
return array(
$abs.'class '.$alias.' extends '.$namespace.'{};'
);
}
}
}