All pastes #562872 Raw Edit

Untitled

public text v1 · immutable
·published 2007-06-12 22:55 UTC
rendered paste body
<?php

require_once('../config/config.php');

require_once(APP_ROOT.'app/lib/db/Database.php');

class CRUDCutter {
    
    protected $dbObj;
    protected $ignoreTables = array('users',
                                    'userpermissions',
                                    'permissions',
                                    'userhistorylog',
                                    'exceptionlogger',
                                    'ws_session',
                                    'useraccesslog');
    
    protected $tables = array();
    protected $controllerTables = array();
    
    public function __construct() {
        
        $this->dbObj = Database::connect();
        $this->findTables();
        $this->findFields();
        
    }
    
    public function getIgnoredTables() {
        
        return $this->ignoreTables;
        
    }
    
    public function ignoreTable( $tableName ) {
        
        $this->ignoreTables[] = $tableName;
        
    }
    
    public function makeController($tableName) {
        
        $this->controllerTables[] = $tableName;
        
    }
    
    protected function findTables() {
        
        $query = "show tables";
        $tables = $this->dbObj->getAll( $query );
        
        if(is_array( $tables )) {
           
           foreach( $tables as $table ) {
                
                $this->tables[] = $table['Tables_in_'.DB_DATABASE.''];    
                
           }
            
        }
        
    }
    
    protected function findFields() {
        
        if( is_array($this->tables)) {
            
            foreach( $this->tables as $table ) {
                
                $query = " DESCRIBE ". $table;
                
                $tblProps[$table] = $this->dbObj->getAll( $query );
                
            }
            
        }
        
        $this->tables = $tblProps;
        
    }
    
    public function generateControllers() {
        
        foreach($this->controllerTables as $table) {
            $varName = substr($table,0,1);
            $controllerFile = '<?php
    
    class '.ucfirst(strtolower($table)).' extends Controller {
        
        public function __construct() {
            
            
        }
        
        public function main() {
            $this->view->setParam("greeting","You made it to '.$table.'");
        }
        
        public function viewAll() {
            $'.$varName.' = new '.$table.'Store();
            $records = $'.$varName.'->read();
            if(is_array($records)) {
                $this->view->setParam("records",$records);
            } else {
                $this->view->setParam("records","There are no records");
            }
        }
        
        public function render() {
            $this->view->render();
        }
        
    }
    
?>';

        $templateFile = '<h1>Records</h1>
<?=$error;?>
<?php

foreach($records as $record) {
    
    echo \'<p>\';
        foreach($record as $index=>$value) {
            echo \'<b>\'.$index.\':</b> \'.$value.\'<br />\';
        }
    echo \'</p>\';
    
}

?>
';
            
            file_put_contents(APP_ROOT.TEMPLATE_DIR.strtolower($table).'/viewall.tpl',$templateFile);
            file_put_contents(APP_ROOT."app/lib/controllers/".ucfirst(strtolower($table)).".php",$controllerFile);
            chmod(APP_ROOT.TEMPLATE_DIR.strtolower($table).'/viewall.tpl', 0664);
            chmod(APP_ROOT."app/lib/controllers/".ucfirst(strtolower($table)).".php", 0664);
            echo "Made controller: ".$table.".php";
            
        }
        
    }
    
    public function generateController() {
        
        if( is_array($this->tables) ) {
            
            $tables = array_keys($this->tables);
            
            foreach($tables as $table) {
                
                if(!in_array($table, $this->ignoreTables)) {
                
                $requires .= '    require_once(APP_ROOT."app/stores/'. ucfirst(strtolower($table)) .'.php");
';
                
            } else {
                unset($this->tables[$table]);
            }}
            
            if(CUSTOM_SESSION) {
                $requires .= '  require_once(APP_ROOT."app/lib/sessions/Session.php");';
            }
            
        }
        
        if(strlen(URL_DIR) > 0) {
            $request = '$request = str_replace(URL_DIR.\'/\', \'\', $request);';
        } else {
            $request = '$request = substr($_SERVER[\'REQUEST_URI\'],1);';
        }
        
        $file = '<?php
        
    /**
     * @author Josh Team <jteam@creditsolutions.com>
     * @package Controller
     * @version 1.0
     */
    
    require_once("'.APP_ROOT.'config/config.php");
    
    require_once(APP_ROOT."app/lib/ajax/Ajax.php");
    require_once(APP_ROOT."app/lib/controllers/Controller.php");
    require_once(APP_ROOT."app/lib/view/View.php");
    require_once(APP_ROOT."app/lib/validate/Validation.php");
    require_once(APP_ROOT."app/lib/log/Log.php");
    require_once(APP_ROOT."app/lib/security/User.php");
    require_once(APP_ROOT."app/lib/db/Database.php");
    require_once(APP_ROOT."app/lib/dispatch/Dispatch.php");
    require_once(APP_ROOT."app/lib/registry/Registry.php");
'. $requires .'
    session_start();
    
    /**
     * Handles all HTTP Request, Assumes that your .htaccess points
     * all request to a file that instantiates this class.
     *
     * The Front Controller handles the following.
     * <ul>
     *  <li>Handling Sessions</li>
     *  <li>Dispatching the Request</li>
     *  <li>Logging Exceptions</li>
     *  <li>Ability to log Method calls</li>
     * </ul>
     *
     * On instantiation of the FrontController The RequestURL is
     * passed into the Dispatch.
     */
    class FrontController {
        
        /**
         * Simply dispatches the request_uri to the Dispatcher
         */
        public function __construct() {
            
            //Dispatch URL Request
            $this->dispatch($_SERVER[\'REQUEST_URI\']);
            
        }
        
        private function dispatch( $r ) {
            
            $request = $r;
            '. $request .'
            $request = explode(\'/\',$request);
            
            if(ucfirst(strtolower($request[0])) != "Controller") {
                
                try {
                    
                    $d = new Dispatch($request);
                    $d->dispatch();
                    
                } catch (Exception $e) {   
                    
                    $this->logException($e);
                    throw new Exception($e->getMessage());
                    
                }
                
            } else {
                
                throw new Exception("You can not call the main Controller through HTTP Request");
                
            }
            
        }
        
        /**
         * For method overloading. If the first 4 characters are log_ it then calls
         * the preeceding method, and stores the method call using FrontController::logAct
         *
         */
        public function __call($m, $a) {
            
            if(substr($m,0,4) == \'log_\') {
                
                $method = substr($m,4);
                
                if(method_exists($this,$method)) {
                    
                    $this->logAct($method, $a);
                    return (call_user_func_array(array($this, $method), $a));
                    
                } else {
                    
                    $this->logException($e);
                    throw new Exception("Couldn\'t find/run: ". __CLASS__ ."::".$method);
                    
                }
                
            }
            
        }
        
        private function logAct($method, $args) {
            
            if(isset($this->user)) {
                
                $userID = $this->user->getID();
                
            } else {
                
                $userID = 0;
                
            }
            
            $l = new Log();
            $l->saveMethodCall( __CLASS__, $method, $args, $userID);
            
        }
        
        private function logException(Exception $e) {
            
            $l = new Log();
            $l->saveException( $e );
            
        }
        
        
    ';
    
    $file .= '  
        
    }


?>';
        
        if(!file_exists(APP_ROOT."app/lib/controllers/FrontController.php")) {
            
            echo "Made File: ".APP_ROOT."app/lib/FrontController.php\n<br />";
            file_put_contents(APP_ROOT."app/lib/controllers/FrontController.php",$file);
            chmod(APP_ROOT."/app/lib/controllers/FrontController.php", 0664);
            
        } else {
            
            throw new Exception("Controller Already Created.");
            
        }
        
        
    }
    
    public function generateDAOPages() {
        
        if( is_array( $this->tables )) {
            
            foreach( $this->tables as $name=>$table ) {
                
                if( !in_array($name,$this->ignoreTables) ) {
                    
                $file  = '<?php
  
    //need to change the crudcutter to read config for includes
    require_once("'.APP_ROOT.'app/lib/crud/CRUD.php");
  
    class '.$name.'Store extends CRUD {
        
        protected $dbObj;
        
        public $props = array(
            ';
    
    foreach($table as $prop) {
        
        if(!preg_match('!(.*?)\((\d*)\)!',$prop['Type'],$matches)) {
            
            $matches[1] = $prop['Type'];
            
        }
        
        $file .= "'".$prop['Field']."'=>array('max_length'=>'".$matches[2]."','type'=>'".$matches[1]."','required'=>'','default'=>'".$prop['Default']."','null'=>'".$prop['Null']."'),
            ";
    
    }
    
$file .='\'propCount\'=>'.count($table).');
        
        public function __construct() {
            $this->dbObj = Database::connect();
        }
        
        
    }
    
?>';
                    
                    if(!file_exists(APP_ROOT."app/stores/".ucfirst(strtolower($name)).".php")) {
                        
                        echo "Made File: ".APP_ROOT."app/stores/".ucfirst(strtolower($name)).".php\n<br />";
                        file_put_contents(APP_ROOT."app/stores/".ucfirst(strtolower($name)).".php",$file);
                        chmod(APP_ROOT."app/stores/".ucfirst(strtolower($name)).".php", 0664);
                        
                    } else {
                        
                        throw new Exception('Data Access Object(s) Already Created: '. ucfirst(strtolower($name)) .'.php');
                        
                    }
                    
                }
                
            }
            
        }
        
    }
    
}

$t = new CRUDCutter();
try {
    $t->makeController('clients');
    $t->generateDAOPages();
    $t->generateController();
    $t->generateControllers();
} catch (Exception $e) {
    
    echo $e->getMessage();
    
}


?>