/
prag
executable file
·22724 lines (19813 loc) · 599 KB
/
prag
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
# Pragwork 1.1.0
# Prag Console Tool 1.1.0
# Author: Szymon Wrozynski
# Copyright (c) 2009-2011 Szymon Wrozynski (http://wrozynski.com)
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
namespace Prag;
const PRAG_VERSION = '1.1.0';
const AUTHOR = 'Copyright (c) 2009-2011 Szymon Wrozynski';
const PRAGWORK_VERSION = '1.1.0';
const LICENSE = 'Licensed under the MIT License';
# Field class (for Model and Scaffolding) ####################################
class Field
{
public $name;
public $type;
private $_nullable = false;
private $_text = false;
public function __construct($declaration)
{
$parts = explode(':', $declaration);
if (count($parts) !== 2)
error("Wrong field declaration: $declaration");
$this->name = ensure_ident($parts[0], true);
if (strpos($parts[1], 'nullable ') === 0)
{
$parts[1] = substr($parts[1], 9);
$this->_nullable = true;
}
if (in_array($parts[1], array('bool', 'int', 'float', 'string'), true))
$this->type = $parts[1];
elseif ($parts[1] === 'text')
{
$this->type = 'string';
$this->_text = true;
}
else
error("Unknown type: {$parts[1]}");
}
public function is_nullable()
{
return $this->_nullable;
}
public function is_bool()
{
return $this->type === 'bool';
}
public function is_int()
{
return $this->type === 'int';
}
public function is_float()
{
return $this->type === 'float';
}
public function is_string()
{
return $this->type === 'string';
}
public function is_text()
{
return $this->_text;
}
public function is_id()
{
return $this->is_int()
&& strrpos($this->name, '_id') === strlen($this->name) - 3;
}
public function declaration()
{
$decl = $this->name . ':';
if ($this->_nullable)
$decl .= 'nullable ';
if ($this->_text)
$decl .= 'text';
else
$decl .= $this->type;
return $decl;
}
}
# Globals ####################################################################
$STRIP_PHPDOC = false;
$SSL = false;
$EXT = '';
$ASTERISK_USED = false;
$WRITE_MSG = null;
$THE_SAME_WRITE_MSG = false;
# Basic functions ############################################################
set_error_handler(function($errno, $errstr, $errfile, $errline)
{
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
});
function read($path)
{
try
{
return file_get_contents($path);
}
catch (\ErrorException $e)
{
error("Cannot find or read $path");
}
}
function write($path, $text, $asterisk=false)
{
global $WRITE_MSG, $THE_SAME_WRITE_MSG, $ASTERISK_USED;
try
{
file_put_contents($path, $text);
$msg = "Writing $path";
if ($asterisk)
{
$ASTERISK_USED = true;
$msg .= ' *';
}
$THE_SAME_WRITE_MSG = ($msg === $WRITE_MSG);
if ($THE_SAME_WRITE_MSG)
echo '.';
else
{
if ($WRITE_MSG !== null)
echo "\n";
$WRITE_MSG = $msg;
echo $msg;
}
}
catch (\ErrorException $e)
{
if ($WRITE_MSG !== null)
echo "\n";
error("Cannot write $path. Are you in the project directory?");
}
}
function write_binary($path, $binary)
{
write($path, base64_decode($binary));
}
// ------------ lixlpixel recursive PHP functions -------------
// recursive_remove_directory( directory to delete, empty )
// expects path to directory and optional TRUE / FALSE to empty
// ------------------------------------------------------------
function rmtree($directory, $empty=false)
{
if (substr($directory, -1) === '/')
$directory = substr($directory, 0, -1);
if (!is_dir($directory))
return false;
elseif (is_readable($directory))
{
$handle = opendir($directory);
while (false !== ($item = readdir($handle)))
{
if ($item !== '.' && $item !== '..')
{
$path = $directory . DIRECTORY_SEPARATOR . $item;
if (is_dir($path))
rmtree($path);
else
unlink($path);
}
}
closedir($handle);
if($empty === false)
{
if(!rmdir($directory))
return false;
}
}
return true;
}
function error($msg)
{
echo "ERROR: $msg\n\n";
exit(1);
}
function check_file_exists($path)
{
if (!is_file($path))
error("Cannot found $path file. Are you in the project directory?");
}
function check_dir_exists($path)
{
if (!is_dir($path))
error("Cannot found $path directory. Are you in the project directory?");
}
function ensure_ident($ident, $strict=false)
{
$pattern = $strict ? '/^[a-z]+[a-z0-9_]*$/' : '/^[a-z]+[\a-z0-9_]*$/';
if (!preg_match($pattern, $ident))
error("Incorrect identifier: $ident");
return $ident;
}
# Various helper functions ###################################################
function _make_dirs($initial_path, $dirs)
{
check_dir_exists($initial_path);
foreach ($dirs as $d)
{
$initial_path .= DIRECTORY_SEPARATOR . $d;
if (!is_dir($initial_path))
mkdir($initial_path, 0755);
}
return $initial_path;
}
function _make_app_dirs(/*...*/)
{
return _make_dirs('app', func_get_args());
}
function _make_test_dirs(/*...*/)
{
return _make_dirs('test', func_get_args());
}
function _make_ns_dirs($path, $ns)
{
if (strlen($ns) === 0)
return;
try
{
foreach (explode('\\', $ns) as $part)
{
$path .= DIRECTORY_SEPARATOR . $part;
if (!is_dir($path))
mkdir($path, 0755);
}
}
catch (\ErrorException $e)
{
error("Cannot create $path. Are you in the project directory?");
}
}
# Taken from PHP ActiveRecord by and Kien La, Jacques Fuentes (MIT License)
function _pluralize($string)
{
$plural = array(
'/(quiz)$/i' => "$1zes",
'/^(ox)$/i' => "$1en",
'/([m|l])ouse$/i' => "$1ice",
'/(matr|vert|ind)ix|ex$/i' => "$1ices",
'/(x|ch|ss|sh)$/i' => "$1es",
'/([^aeiouy]|qu)y$/i' => "$1ies",
'/(hive)$/i' => "$1s",
'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
'/(shea|lea|loa|thie)f$/i' => "$1ves",
'/sis$/i' => "ses",
'/([ti])um$/i' => "$1a",
'/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
'/(bu)s$/i' => "$1ses",
'/(alias)$/i' => "$1es",
'/(octop)us$/i' => "$1i",
'/(ax|test)is$/i' => "$1es",
'/(us)$/i' => "$1es",
'/s$/i' => "s",
'/$/' => "s"
);
$irregular = array(
'move' => 'moves',
'foot' => 'feet',
'goose' => 'geese',
'sex' => 'sexes',
'child' => 'children',
'man' => 'men',
'tooth' => 'teeth',
'person' => 'people'
);
$uncountable = array(
'sheep',
'fish',
'deer',
'series',
'species',
'money',
'rice',
'information',
'equipment'
);
if (in_array(strtolower($string), $uncountable))
return $string;
foreach ($irregular as $pattern => $result)
{
$pattern = '/' . $pattern . '$/i';
if (preg_match($pattern, $string))
return preg_replace($pattern, $result, $string);
}
foreach ($plural as $pattern => $result)
{
if (preg_match($pattern, $string))
return preg_replace($pattern, $result, $string);
}
return $string;
}
function _camelize($name, $separator='_')
{
return str_replace(array('_', ' '), array('', ''), ucwords(str_replace(
array($separator, '\\'), array(' _ ', ' \\ '), trim($name))));
}
function _uncamelize($name, $separator='_')
{
$result = '';
$prev = '';
$i = 0;
$special_chars = array('_', '\\');
$name = trim($name);
while(isset($name[$i]))
{
if (ctype_upper($name[$i]) && $prev && !in_array($prev, $special_chars, true))
$result .= $separator;
$result .= strtolower($name[$i]);
$prev = $name[$i++];
}
return $result;
}
function _get_ns_and_name($name)
{
$rpos = strrpos($name, '\\');
return ($rpos > -1) ? array(substr($name, 0, $rpos), substr($name, $rpos + 1)) : array('', $name);
}
function _strip_phpdoc($text)
{
if ($GLOBALS['STRIP_PHPDOC'])
{
$comment_start = strpos($text, '/**');
if ($comment_start > 0)
{
$comment_end = strpos($text, '*/', $comment_start);
if ($comment_end > 0)
return _strip_phpdoc(substr($text, 0, $comment_start) . substr($text, $comment_end + 2));
}
}
return $text;
}
# Main generator functions ###################################################
function new_actions_with_methods($full_controller_sn, $actions_with_methods, &$prepend_comment)
{
$http_methods = array('GET', 'POST', 'PUT', 'DELETE');
$ams = array();
$ams_size = 0;
foreach ($actions_with_methods as $entry)
{
if (in_array($entry, $http_methods, true))
{
if (!$ams)
error("An action should be specified before the $entry method");
if (!in_array($entry, $ams[$ams_size - 1]['methods']))
$ams[$ams_size - 1]['methods'][] = $entry;
}
else
$ams[$ams_size++] = array('action' => $entry, 'methods' => array());
}
foreach ($ams as $am)
new_action(
$full_controller_sn,
$am['action'],
$am['methods'] ? join(' ', $am['methods']) : 'GET',
$prepend_comment
);
}
function new_action($full_controller_sn, $action, $methods, &$prepend_comment)
{
$path = 'app' . DIRECTORY_SEPARATOR . 'Controllers' . DIRECTORY_SEPARATOR
. str_replace('\\', DIRECTORY_SEPARATOR, _camelize($full_controller_sn)) . 'Controller.php';
check_file_exists($path);
$controller_text = read($path);
if (preg_match('/\s*public\s+function\s+' . $action . '\s*\(\s*\)\s*\{/', $controller_text))
return;
$mapping = str_replace(array('_','\\'), array('-','/'), $full_controller_sn) . '/' . str_replace('_', '-', $action);
new_route($full_controller_sn, $action, $methods,$mapping,$prepend_comment);
$first_part = substr($controller_text, 0, strrpos($controller_text, '}'));
global $EXT;
$new_controller_text = $first_part . PHP_EOL . "\t# $methods /$mapping$EXT" . PHP_EOL
. "\tpublic function $action()" . PHP_EOL . "\t{}" . PHP_EOL . '}' . PHP_EOL . '?>';
write($path, $new_controller_text);
new_view($full_controller_sn, $action);
}
function new_route($full_controller_sn, $action, $methods, $mapping, &$prepend_comment)
{
$path = 'config' . DIRECTORY_SEPARATOR . 'routes.php';
check_file_exists($path);
$routes_text = read($path);
$first_entry = ($routes_text === <<< 'EOD'
<?php
$ROUTES = array(
);
?>
EOD
);
if (preg_match('/\s*\'controller\'\s*=>\s*\'' . str_replace('\\', '\\\\', _camelize($full_controller_sn))
. '\'\s*,\s*\'action\'\s*=>\s*\'' . $action . "'/", $routes_text))
return;
if ($prepend_comment && ! $first_entry)
{
$comment = PHP_EOL . "\t# " . $prepend_comment . PHP_EOL;
$prepend_comment = null;
}
else
$comment = '';
$routes_text = substr($routes_text, 0, strrpos($routes_text, PHP_EOL . ');' . PHP_EOL . '?>'));
global $SSL, $EXT;
$asterisk = $SSL || $EXT;
$ssl_line = $SSL ? ',' . PHP_EOL . "\t\t'ssl' => true" : '';
if ($first_entry)
{
$routes_text .= PHP_EOL . "\t# Root";
$mapping_line = '';
}
else
{
$routes_text .= ',';
$mapping_line = PHP_EOL . "\t\t'/" . $mapping . $EXT . "',";
}
$routes_text .= PHP_EOL . $comment . "\tarray($mapping_line" . PHP_EOL
. "\t\t'controller' => '" . _camelize($full_controller_sn) . "'," . PHP_EOL
. "\t\t'action' => '$action'," . PHP_EOL
. "\t\t'methods' => '$methods'$ssl_line" . PHP_EOL
. "\t)" . PHP_EOL . ');' . PHP_EOL . '?>';
write($path, $routes_text, $asterisk);
}
function new_view($full_controller_sn, $action)
{
$dir_path = _make_app_dirs('views');
$full_ns = _camelize($full_controller_sn);
_make_ns_dirs($dir_path, $full_ns);
$dirs = str_replace('\\', DIRECTORY_SEPARATOR, $full_ns);
$path = $dir_path . DIRECTORY_SEPARATOR . $dirs . DIRECTORY_SEPARATOR . $action . '.php';
if (is_file($path))
return;
$place = 'app' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $dirs . DIRECTORY_SEPARATOR . $action.'.php';
write($path, sprintf(
'<h1>%s\%s</h1>' . PHP_EOL . PHP_EOL . '<p>Find me in %s</p>',
_camelize($full_controller_sn),
$action,
$place
));
}
function new_controller($full_controller_sn, $actions_with_methods, &$prepend_comment)
{
$path = _make_app_dirs('Controllers');
$test_path = _make_test_dirs('Controllers');
list($ns, $cont) = _get_ns_and_name($full_controller_sn);
_make_ns_dirs($path, _camelize($ns));
_make_ns_dirs($test_path, _camelize($ns));
$path .= DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, _camelize($full_controller_sn))
. 'Controller.php';
$test_path .= DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, _camelize($full_controller_sn))
. 'ControllerTest.php';
$ns_clause = $ns ? '\\' . _camelize($ns) : '';
$abstract = $actions_with_methods ? '' : 'abstract ';
$use_app_cont = $ns_clause ? 'use Controllers\ApplicationController;' . PHP_EOL : '';
$text = '<?php' . PHP_EOL . 'namespace Controllers' . $ns_clause . ';' . PHP_EOL . $use_app_cont . PHP_EOL
. $abstract . 'class ' . _camelize($cont) . 'Controller extends ApplicationController'
. PHP_EOL . '{}' . PHP_EOL . '?>';
$test_text = sprintf(<<< 'TC'
<?php
namespace Controllers%s;
modules('test');
class %sControllerTest extends \Test\ControllerTestCase
{
public function test_the_truth()
{
ensure(true);
}
}
?>
TC
, $ns_clause, _camelize($cont));
write($path, $text);
if (!$abstract && !is_file($test_path))
write($test_path, $test_text);
new_helper($ns, $cont);
new_actions_with_methods($full_controller_sn, $actions_with_methods,
$prepend_comment);
}
function new_helper($namespace_sn, $controller_sn)
{
$path = _make_app_dirs('helpers');
$camelized_ns = _camelize($namespace_sn);
_make_ns_dirs($path, $camelized_ns);
$path .= DIRECTORY_SEPARATOR
. str_replace('\\', DIRECTORY_SEPARATOR, $camelized_ns)
. _camelize($controller_sn) . 'Helper.php';
write($path, '');
}
# Help printing function #####################################################
function help()
{
printf(<<< HELPMSG
USAGE:
php prag work name
- Generates a new project.
php prag help[|h]
- Prints this information.
Commands available in the project directory:
php prag [ssl] [.ext] controller[|c] name
php prag [ssl] [.ext] controller[|c] namespace\\\\name action_1 [action_2] ...
php prag [ssl] [.ext] controller[|c] name action_1 [GET POST PUT DELETE] ...
- Generates a new controller with given actions and HTTP methods
or adds new actions to the existing one. This command also adds
the default routes to "routes.php" file and creates the default
view templates. The 4 HTTP methods are allowed: GET, POST, PUT, DELETE.
They should be passed in upper case and follow the action.
If no HTTP method follows the action then the GET method is assumed.
This command may be prepended with the "ssl" particle. It causes
all generated routes to have the "ssl" option set to "true".
Also, this command may be prepended with an extension starting with a dot.
The extension will be appended to all generated routes.
Examples:
php prag .html controller my_controller get_post GET POST post_only POST
php prag c my_controller get_post GET POST post_only POST
php prag [ssl] [.ext] scaffold[|s][!] entity field_1:type [field_2:type] ...
- Generates a scaffolding around a given model using ActiveRecord module.
This command creates a controller, view templates, model class,
and routing entries for CRUD actions.
The Pragwork uses 5 predefined field types: "bool", "float", "int",
"string", and "text". Each type can be prepended with the keyword
"nullable" if it is allowed to hold a null value. The field name
and type are separated with a colon.
If the field type is an "int" and its name ends with "_id" then
the "INT UNSIGNED" is used within the SQL generated code. This is
because of an assumption that the field acts as a foreign key in
the relationship between tables.
To overwrite existing files, add an exclamation mark (!) to the command.
This command may be prepended with the "ssl" particle. It causes
all generated routes to have the "ssl" option set to "true".
Also, this command may be prepended with an extension starting with a dot.
The extension will be appended to all generated routes.
Examples:
php prag scaffold person name:string last_name:string age:int
php prag s! person name:string last_name:string age:int
php prag model[|m][!] entity field_1:type [field_2:type] ...
- Creates (or overwrites) model class for the given entity.
Generates the SQL code if needed. Follows exactly the same
syntax as the "scaffold" command.
To overwrite existing files, add an exclamation mark (!) to the command.
php prag form[|f][!] entity field_1:type [field_2:type] ...
- Creates a tableless model class used as a plain model without
a database connection. It is especially useful within forms because of
the ActiveRecord validation features. Also, this command generates the
corresponding form partial template.
The command follows exactly the same syntax as the "scaffold" command.
To overwrite existing files, add an exclamation mark (!) to the command.
Examples:
php prag form! login name:string password:string
php prag f login name:string password:string
php prag localize[|l][!] [locale_code] [locale_code_2] ...
- Creates localization files according to specified locale codes. Also, this
command appends stub entries based on possible keys found in the code
to the localization files.
If no locale codes were specified all available locales are evaluated.
To overwrite existing files, add an exclamation mark (!) to the command.
Examples:
php prag localize!
php prag l en pl
php prag db prepare [all|development|test|production|other...] name [password]
- Creates database for all environments or for the chosen one. Also, this
command creates writes the default schema and data to the database.
Requires database root user name and password.
Warning: This command will erase the whole database prior to the creation.
Example:
php prag db prepare all root root
Example without password:
php prag db prepare development root
php prag db schema [all|development|test|production|other...]
- Writes the default schema to the databases in all environments or in the
chosen one.
Warning: Probably all your data existing in the database will be erased.
php prag db data [all|development|test|production|other...]
- Writes the default data to the databases in all environments or in the
chosen one.
Warning: Probably all your data existing in the database will be erased.
php prag test[|t] [fixture_name] [fixture_name_2] ...
- Runs tests available in the 'test' directory. If no fixture is given then
all fixtures are run one by one. Test are performed in the 'test'
environment and the test database is filled with default data before each
test fixture.
php prag update [nodoc]
- Updates modules of the current project to Pragwork %s.
The optional "nodoc" parameter causes writing modules without
PHPDoc comments. That makes some modules significantly smaller and
therefore they can be loaded a bit faster.
HELPMSG
, PRAGWORK_VERSION);
}
# Localization functions
function _make_locale_file($locale)
{
$path = 'locales' . DIRECTORY_SEPARATOR . $locale . '.php';
write($path, sprintf(<<< 'EOD'
<?php
$LOCALE = array(
'%s'
);
?>
EOD
, $locale));
}
function _add_locale_stub($locale, $key, &$prepend_comment, $value='null')
{
$path = 'locales' . DIRECTORY_SEPARATOR . $locale . '.php';
if (($key[0] === '"') && (strpos($key, "'") === false))
$key = "'" . trim($key, '"') . "'";
$locale_text = read($path);
preg_match_all('/\s*(\'[^\\\']+\'|"[^\\"]+")\s*=>/', $locale_text, $matches);
if (isset($matches[1]) && in_array($key, $matches[1], true))
return;
if ($prepend_comment)
{
$comment = PHP_EOL . "\t# " . $prepend_comment . PHP_EOL;
$prepend_comment = null;
}
else
$comment = '';
$first_part = substr($locale_text, 0, strrpos($locale_text, PHP_EOL . ');' . PHP_EOL . '?>'));
$new_locale_text = $first_part . ',' . PHP_EOL . "$comment\t$key => $value" . PHP_EOL . ');' . PHP_EOL . '?>';
write($path, $new_locale_text);
}
function _find_locale_keys_in_dir($locale, $directory, &$prepend_comment)
{
foreach (scandir($directory) as $entry)
{
if ($entry[0] === '.')
continue;
$entry = $directory . DIRECTORY_SEPARATOR . $entry;
if (is_dir($entry))
_find_locale_keys_in_dir($locale, $entry, $prepend_comment);
else
{
$keys = array();
preg_match_all('/[^\w$][tT]\s*\(\s*(\'[^\\\']+\'|"[^\\"]+")\s*\)/', read($entry), $keys);
foreach ($keys[1] as $key)
_add_locale_stub($locale, $key, $prepend_comment);
}
}
}
function _find_model_validation_messages_in_dir($locale, $directory, &$keys)
{
foreach (scandir($directory) as $entry)
{
if ($entry[0] === '.')
continue;
$entry = $directory . DIRECTORY_SEPARATOR . $entry;
if (is_dir($entry))
_find_model_validation_messages_in_dir($locale, $entry, $keys);
else
{
$text = read($entry);
$matches = array();
preg_match_all('/\s*(\'message\'|"message"|\'wrong_length\'|"wrong_length"|\'too_long\'|"too_long"|\'too_short\'|"too_short")\s*=>\s*(\'[^\\\']+\'|"[^\\"]+")/', $text, $matches);
if (isset($matches[2]))
{
if (is_array($matches[2]))
$keys = array_merge($keys, $matches[2]);
else
$keys[] = $matches[2];
}
$matches = array();
preg_match_all('/\$this\s*->\s*errors\s*->\s*add\s*\(\s*(\'\w+\'|"\w+")\s*,\s*(\'[^\\\']+\'|"[^\\"]+")\s*\)/', $text, $matches);
if (isset($matches[2]))
{
if (is_array($matches[2]))
$keys = array_merge($keys, $matches[2]);
else
$keys[] = $matches[2];
}
}
}
}
function _find_model_validation_messages($locale, &$prepend_comment)
{
$keys = array(
"'is not included in the list'",
"'is reserved'",
"'is invalid'",
'"can\'t be empty"',
'"can\'t be blank"',
"'is too long (maximum is %d characters)'",
"'is too short (minimum is %d characters)'",
"'is the wrong length (should be %d characters)'",
"'is not a number'",
"'must be greater than %d'",
"'must be equal to %d'",
"'must be less than %d'",
"'must be odd'",
"'must be even'",
"'must be unique'",
"'must be less than or equal to %d'",
"'must be greater than or equal to %d'"
);
$path = 'app' . DIRECTORY_SEPARATOR . 'Models';
if (is_dir($path))
_find_model_validation_messages_in_dir($locale, $path, $keys);
foreach ($keys as $key)
_add_locale_stub($locale, $key, $prepend_comment);
}
function _find_model_fields_in_dir($locale, $directory, &$keys)
{
foreach (scandir($directory) as $entry)
{
if ($entry[0] === '.')
continue;
$entry = $directory . DIRECTORY_SEPARATOR . $entry;
if (is_dir($entry))
_find_model_fields_in_dir($locale, $entry, $keys);
else
{
$text = read($entry);
$matches = array();
preg_match_all('/\s*public\s+\$(\w+)\s*[=;]/', $text, $matches);
if (isset($matches[1]))
{
if (is_array($matches[1]))
$keys = array_merge($keys, $matches[1]);
else
$keys[] = $matches[1];
}
$matches = array();
preg_match_all('/\s*public\s+function\s+set_(\w+)\s*\(/', $text, $matches);
if (isset($matches[1]))
{
foreach ($matches[1] as $k)
{
if (preg_match('/\s*public\s+function\s+get_' .$k. '\s*\(/', $text))
$keys[] = $k;
}
}
}
}
}
function _find_model_fields($locale, &$prepend_comment)
{
$keys = array();
$path = 'app' . DIRECTORY_SEPARATOR . 'Models';
if (is_dir($path))
_find_model_fields_in_dir($locale, $path, $keys);
foreach($keys as $key)
_add_locale_stub($locale, "'" . $key . "'", $prepend_comment,
"'" . str_replace('_', ' ', ucfirst($key)) . "'");
}
function _find_model_fields_in_sql($locale, &$prepend_comment)
{
$path = 'sql' . DIRECTORY_SEPARATOR . 'schema.sql';
check_file_exists($path);
$matches = array();
preg_match_all('/\s+(\w+)\s*:\s*[a-zA-Z]+/', read($path), $matches);
if (isset($matches[1]) && $matches[1])
{
_add_locale_stub($locale, "'id'", $prepend_comment, "'Id'");
foreach ($matches[1] as $attr)
{
$attr = _uncamelize($attr);
_add_locale_stub($locale, "'" . $attr . "'", $prepend_comment, "'".str_replace('_',' ',ucfirst($attr))."'");
}
_add_locale_stub($locale, "'created_at'", $prepend_comment, "'Created at'");
_add_locale_stub($locale, "'updated_at'", $prepend_comment, "'Updated at'");
}
}
function find_locale_keys_and_create_locale($locale, $overwrite)
{
check_dir_exists('locales');
check_dir_exists('app');
check_dir_exists('config');
check_dir_exists('errors');
$path = 'locales' . DIRECTORY_SEPARATOR . $locale . '.php';
if ($overwrite && is_file($path))
unlink($path);
if (!is_file($path))
_make_locale_file($locale);
$comment = 'Fields';
_find_model_fields_in_sql($locale, $comment);
_find_model_fields($locale, $comment);
$comment = 'Configuration entries';
_find_locale_keys_in_dir($locale, 'config', $comment);
$comment = 'Model validation messages';
_find_model_validation_messages($locale, $comment);
$comment = 'Messages';
_find_locale_keys_in_dir($locale, 'errors', $comment);
_find_locale_keys_in_dir($locale, 'app', $comment);