class ' . $class_name . ' { }'); }); /** * Creates a new controller which inherits the RespondController */ Workbench::register('^new:controller ([a-zA-Z0-9\\_\\.]+) json$', function ($argv) { $name = $argv[1]; $class_name = $name; $dot_pos = strrpos($name, '.'); // get and create containing directory + get classname if ($dot_pos !== false) { $dir = Core\App::controllers()->directory(substr($name, 0, $dot_pos)); if (!$dir->exists()) { $dir->create(); } $class_name = substr($name, $dot_pos + 1); } // create controller file $controller = Core\App::controllers()->file($name . '.php'); $controller->write('<?php namespace ' . Workbench::namespace(Workbench::get('application')) . '; class ' . $class_name . ' extends Blueprints\\RespondController { //The RespondController adds a protected method called respond //which you can use to return JSON data with a success parameter (boolean) //a status parameter with a code, hex code, status name and status message //and a data parameter which you can use to pass extra data //all info about the RespondController can be found in the documentation }'); });
/** * Loads a controller * * @param string $controllerName * @param array $data * @param string|boolean $ns * @param boolean $createInstance * * @return controller|boolean */ public static function load($controllerName, $data = [], $ns = false) { // explode it on @ -> 0 will be the directive + namespace + classname // >1 index will be methods $at_explode = explode('@', $controllerName); $controllerName = $at_explode[0]; // remove the model name from the array array_splice($at_explode, 0, 1); // set default namespace if ($ns === false) { $ns = App::namespace(); } // extract directive $dot_pos = strrpos($controllerName, '.'); $directive = $dot_pos === false ? false : substr($controllerName, 0, $dot_pos) . '.'; // now only contains the classname and namespace if ($dot_pos !== false) { $controllerName = substr($controllerName, $dot_pos + 1); } // register name should be the classname and namespace of the original load $registerName = $controllerName; // extract namespace and classname $back_pos = strrpos($controllerName, '\\'); // namespace now contains default namespace or specified namespace $ns = $back_pos === false ? $ns : substr($controllerName, 0, $back_pos); // modelName now contains the classname if ($back_pos !== false) { $controllerName = substr($controllerName, $back_pos + 1); } // get file path and start inclusion $modelPath = $directive . $controllerName; if (self::exists($modelPath)) { $className = trim($ns, '\\') . '\\' . $controllerName; FileManager::include(App::controllers()->file($modelPath . '.php')); self::$_names[] = $registerName; self::$_name_bindings[$registerName] = $className; //data was passed if ($data != null && count($data) > 0) { if (method_exists($className, 'set')) { foreach ($data as $key => $value) { call_user_func([$className, 'set'], $key, $value); } } } // execute requested @ functions // multiple methods can be called using multiple @ symbols $return_data = []; $return_data_keys = []; foreach ($at_explode as $method) { if (method_exists($className, $method)) { $return = call_user_func([$className, $method]); if (is_array($return)) { $return_data[$method] = $return; $return_data_keys[] = $method; } } } // echo array if any data $return_data_count = count($return_data); if ($return_data_count == 1) { echo json_encode($return_data[$return_data_keys[0]]); } elseif ($return_data_count > 1) { echo json_encode($return_data); } return true; } return false; }