Beispiel #1
0
 /**
  * undocumented 
  *
  * @author Joshua Davey
  */
 public function initialize2()
 {
     array_shift($_SERVER['argv']);
     $args = $_SERVER['argv'];
     $scriptName = false;
     $commandName = false;
     // get list of available consoles
     $scriptNames = array('create', 'delete', 'test', 'migrate');
     while (true) {
         // reset console
         $scriptName = false;
         // check to see if the console exists in the args
         if (isset($args[0])) {
             $scriptName = array_shift($args);
         }
         // if the command requires to be in the application's root path then check it.
         // If we aren't in the applicatin's root path then tell the user and exit
         if ($scriptName != 'make') {
             if (!file_exists(realpath('application/vendor/Madeam.php'))) {
                 console\CLI::outError('Please point Madeam Console to the root directory of your application.');
                 exit;
             }
         }
         try {
             $scriptClassName = 'Console_Script_' . ucfirst($scriptName);
             $script = new $scriptClassName();
             break;
         } catch (exception\AutoloadFail $e) {
             if ($scriptName === false) {
                 // ask them for the script of the console they'd like to use
                 console\CLI::outMenu('Scripts', $scriptNames);
                 $scriptName = console\CLI::getCommand('script');
             }
             // by entering a console name at this point it means they've tried entering one that doesn't exist.
             // prompt them with an saying please try again.
             if ($scriptName !== false) {
                 console\CLI::outError("Sorry the script you entered does not exist.");
             }
         }
     }
     // get list of commands for this script
     $commandNames = array();
     $classReflection = new \ReflectionClass($script);
     foreach ($classReflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $mehodReflection) {
         $commandNames[] = $mehodReflection->getName();
     }
     do {
         // by entering a console name at this point it means they've tried entering one that doesn't exist.
         // prompt them with an saying please try again.
         if ($commandName !== false) {
             console\CLI::outError("Sorry the command you entered does not exist.");
         }
         // reset command
         $commandName = false;
         // check to see if the command exists in the args
         if (isset($args[0])) {
             $commandName = array_shift($args);
         }
         if ($commandName == null) {
             console\CLI::outMenu('Commands', $commandNames);
             $commandName = console\CLI::getCommand('command');
         }
     } while (!in_array($commandName, $commandNames));
     try {
         return $script->{$commandName}($this->parseArguments($args));
     } catch (Exception $e) {
         Exception::handle($e);
     }
     // unset arguments -- they are only for first time use
     $args = array();
 }
Beispiel #2
0
 /**
  * undocumented 
  *
  * @author Joshua Davey
  * @param array $request
  */
 public static function control($request)
 {
     // because we allow controllers to be grouped into sub folders we need to recognize this when
     // someone tries to access them. For example if someone wants to access the 'admin/index' controller
     // they should be able to just type in 'admin' because 'index' is the default controller in that
     // group of controllers. To make this possible we check to see if a directory exists that is named
     // the same as the controller being called and then append the default controller name to the end
     // so 'admin' becomes 'admin/index' if the admin directory exists.
     // note: there is a consequence for this feature which means if you have a directory named 'admin'
     // you can't have a controller named 'Controller_Admin'
     if (is_dir('application/controllers/' . ucfirst($request['_controller']))) {
         $request['_controller'] .= '/' . 'index';
     }
     // set controller's class
     $controllerClassNodes = explode('/', $request['_controller']);
     foreach ($controllerClassNodes as &$node) {
         $node = Inflector::camelize($node);
     }
     $controllerClassNodes[count($controllerClassNodes) - 1] = ucfirst($controllerClassNodes[count($controllerClassNodes) - 1]);
     // set controller class
     $controllerClass = implode('\\', $controllerClassNodes) . 'Controller';
     try {
         $controller = new $controllerClass();
     } catch (Exception\AutoloadFail $e) {
         if (is_dir(self::$pathToProject . 'application/views/' . $request['_controller'])) {
             $view = $request['_controller'] . '/' . $request['_action'];
             $request['_controller'] = 'app';
             $controller = new \AppController();
             $controller->view($view);
         } elseif (is_file(self::$pathToProject . 'application/views/' . $request['_controller'] . '/' . $request['_action'] . '.' . $request['_format'])) {
             $view = $request['_controller'];
             $request['_action'] = $request['_controller'];
             $request['_controller'] = 'app';
             $controller = new \AppController();
             $controller->view($view);
         } else {
             // no controller or view found = critical error.
             //header("HTTP/1.1 404 Not Found");
             Exception::handle($e, array('message' => 'Missing Controller <strong>' . $controllerClass . "</strong> \n Create File: <strong>application/controllers/" . str_replace('_', DS, $controllerClass) . ".php</strong> \n <code>&lt;?php \n class {$controllerClass} extends AppController {\n\n  &nbsp; public function " . Inflector::camelize(lcfirst($request['_action'])) . "Action() {\n &nbsp;&nbsp;&nbsp; \n &nbsp; }\n\n   }</code>"));
             return;
         }
     }
     try {
         // process request
         $response = $controller->process($request);
         // delete controller
         unset($controller);
         // return response
         return $response;
     } catch (controller\exception\MissingAction $e) {
         //header("HTTP/1.1 404 Not Found");
         Exception::handle($e);
         return;
     } catch (controller\exception\MissingView $e) {
         header("HTTP/1.1 404 Not Found");
         Exception::handle($e);
         return;
     }
 }