Source for file crudify.php

Documentation is available at crudify.php

  1. #!/usr/local/bin/php
  2. <?php
  3.  
  4. if(count($argv)<2){
  5.     echo "\nNot enough parameters.\n";
  6.     help();
  7.     return 1;
  8. }
  9.  
  10. for($x=1;$x<count($argv);$x++){
  11.     $arg=$argv[$x];
  12.     if($arg == '-d'){
  13.         define ('DOCTRINE_PATH',$argv[++$x]);
  14.         continue;
  15.     }
  16.     if($arg == '-c'){
  17.         define ('CRUD_CLASS',$argv[++$x]);
  18.         continue;
  19.     }
  20.  
  21.     if($arg == '-w'){
  22.         define ('CRUD_WIDGET',$argv[++$x]);
  23.         continue;
  24.     }
  25.  
  26.     if($arg == '-l'){
  27.         define('LAYOUT_GENERATOR'$argv[++$x]);
  28.         continue;
  29.     }
  30.  
  31.     if($arg == '-g'){
  32.         define('WIDGET_GENERATOR'$argv[++$x]);
  33.         continue;
  34.     }
  35.  
  36.  
  37.  
  38.     else{
  39.         define('CRUD_FILE',$arg);
  40.         if(!defined('CRUD_CLASS')){
  41.             $class=explode('.',basename($arg));
  42.             $class=$class[0];
  43.             define('CRUD_CLASS',$class);
  44.         }
  45.     }
  46.  
  47. }
  48.  
  49. if(!defined('CRUD_FILE')){
  50.     help();
  51.     exit(1);
  52. }
  53. if(!defined('CRUD_WDIGET')){
  54.     define('CRUD_WIDGET',CRUD_CLASS.'Widget');
  55. }
  56.  
  57.  
  58. if(!defined('WIDGET_GENERATOR')){
  59.     define('WIDGET_GENERATOR','DefaultWidget');
  60. }
  61. if(!file_exists(DOCTRINE_PATH.'/lib/Doctrine.php')){
  62.     echo "Doctrine.php not found in".DOCTRINE_PATH.'/lib';
  63.     return 1;
  64. }
  65.  
  66. require_once(DOCTRINE_PATH.'/lib/Doctrine.php');
  67. spl_autoload_register(array('Doctrine''autoload'));
  68. Doctrine_Manager::connection('mysql://root:elizabeth@localhost/ihaveskill');
  69.     echo "File ".CRUD_FILE.'not found or is not readable.';
  70.     return 1;
  71. }
  72.  
  73. require CRUD_FILE;
  74. $r=new $c();
  75. $t=$r->getTable();
  76. $columns=$t->getColumns();
  77.  
  78. //build the widget
  79. require_once(dirname(__FILE__)."/WidgetGenerator.php");
  80. require_once(dirname(__FILE__)."/widgets/".WIDGET_GENERATOR.".php");
  81. $class WIDGET_GENERATOR;
  82. $widgetGen new $class();
  83. $buffer=$widgetGen->generate($columns);
  84.  
  85.  
  86.  
  87. if(!file_put_contents(CRUD_WIDGET.".php",$buffer)){
  88.     echo "\n Error writing to file"CRUD_OUTPUT;
  89. }
  90.  
  91.  
  92. //build the template
  93. if(!is_dir("./templates"))mkdir("./templates");
  94. require_once(dirname(__FILE__)."/LayoutGenerator.php");
  95. $class defined('LAYOUT_GENERATOR'LAYOUT_GENERATOR "DefaultLayout";
  96. require_once(dirname(__FILE__)."/layouts/$class.php");
  97. $layoutGen new $class();
  98. $buffer=$layoutGen->generate($columns);
  99. if(!file_put_contents('./templates/'.CRUD_WIDGET."Edit.tpl",$buffer)){
  100.     echo "\n Error writing to file/templates/".CRUD_WIDGET."Edit.tpl";
  101. }
  102.  
  103.  
  104.  
  105. /**
  106.  * returns the index for the primary key column
  107.  * 
  108.  * @param mixed $array 
  109.  * @access public
  110.  * @return void 
  111.  */
  112. function getPrimaryKey($array){
  113.     foreach((array)$array as $k=>$val){
  114.         if(isset($val['primary']&& $val['primary']==true){
  115.             return $k;
  116.         }
  117.     }
  118.     return false;
  119. }
  120.  
  121. function help(){
  122. echo "USAGE: crudify -d [PATH_TO_DOCTRINE] {options} RECORD_FILE_NAME\n ";
  123. }

Documentation generated on Fri, 11 Jul 2008 13:28:45 -0400 by phpDocumentor 1.4.2