/** * Checks whether the view exists * * @param string $view * * @return boolean */ public static function exists($view) { if (App::views()->file($view . '.php')->exists() || App::views()->file($view . '.wow.php', 2)->exists()) { return true; } return false; }
/** * Gets a directory from the public directory * * @param string $dirName * * @return Directory */ public static function public($dirName) { $path = App::public(); $dir_parts = explode('.', $dirName); //foreach loop is possible here $path .= '/' . implode('/', $dir_parts); return new Directory($path); }
/** * Writes a log * * @param StatusCode $err * @param string $additional */ public static function write($err, $additional = '') { $dt = new DateTime(); $file = App::logs()->file('log_' . $dt->format('Y-m-d') . '.log'); $log = $dt->format('H:i:s') . "\t" . $err->status() . "\t{$additional}\r\n"; if ($file->exists()) { $file->append($log); } else { $file->write($log); } self::clean(); }
/** * Gets a file from the public directory * * @param string $fileName * @param integer $ext_count * * @return File */ public static function public($fileName, $ext_count = 1) { $path = App::public(); $file_parts = explode('.', $fileName); //for loop here since we need to exclude the last part of the array -> extension $file_parts_count = count($file_parts) - $ext_count; $path .= '/' . implode('/', array_slice($file_parts, 0, $file_parts_count)); //add extension part if ($ext_count > 0) { $path .= '.' . implode('.', array_slice($file_parts, $file_parts_count)); } return new File($path); }
public function execute() { //this is what happens when a view is executed $path = $this->_filePath; if ($this->_isWow) { $path = Wow::view($this->_filePath, App::layouts(), App::modules()); if ($this->_version !== false) { $dot_pos = strrpos($path, '.', -5); $path = substr($path, 0, $dot_pos) . '.v' . $this->_version . '.php'; } } FileManager::include($path); }
/** * Creates a backup if needed or forced * * @param boolean $force */ public static function create($force = false) { if (!self::needs() && !$force) { return false; } $bakdir = App::backups(); $bak = new Archive($bakdir->file(time() . '-' . date('YmdHis') . '.zip')); //add system files $sysdir_len = strlen(App::system()->parent()); $files = App::system()->files(true); foreach ($files as $file) { if ($file->parent() != $bakdir) { $bak->addFile($file, substr($file, $sysdir_len + 1)); } } //add public $pubdir_len = strlen(App::public()->parent()); $files = App::public()->files(true); foreach ($files as $file) { $bak->addFile($file, substr($file, $pubdir_len + 1)); } $bak->save(); self::clean(); }
/** * Checks whether the request uri is a match * * @param string $request * * @return boolean */ public function match($request) { if (is_array($this->_route) && isset($this->_route['environment']) && $this->_route['environment'] != App::environment()) { return false; } if (is_string($this->_handle)) { //split handle for multi handle $handles = explode('|', $this->_handle); foreach ($handles as $handle) { $handle_matched = true; //clear previous data Request::clear(); //ignore starting and trailing slashes $q_index = strpos($request, '?'); if ($q_index === false) { $ex_request = explode('/', trim($request, '/')); } else { $ex_request = explode('/', trim(substr($request, 0, $q_index), '/')); $ex_request[count($ex_request) - 1] .= substr($request, $q_index); } $ex_handle = explode('/', $handle); //if the amount of parts dont comply just, end if (count($ex_request) != count($ex_handle)) { continue; } //check all parts of the handle and see whether they match up to the request $ex_count = count($ex_handle); $rx_matches; $i = 0; while ($i < $ex_count) { //check $handle_part = $ex_handle[$i]; $request_part = $ex_request[$i]; //check {get} parameter first if ($i == $ex_count - 1) { //checking the last part of the handle //+{get} if (preg_match("/^(.*)(\\+{get})\$/i", $handle_part)) { //$handle_part ends with +{get} //thus get parameters are allowed //get rid of +{get} in the handle $handle_part = substr($handle_part, 0, strlen($handle_part) - 6); //get rid of anything after first question mark in the request part $qm_pos = strpos($request_part, '?'); if ($qm_pos !== false) { //remove GET part from URL $request_part = substr($request_part, 0, $qm_pos); } } } //check up //most complicated structure -> regexed URL variable if (preg_match("/^(rx)(\\{)([a-zA-Z0-9_]+)(\\})(\\{)(.*)(\\})\$/", $handle_part, $rx_matches) && preg_match("/^" . $rx_matches[6] . "\$/", $request_part)) { //regex for URL variable matches and handle is a regexed variable //setData on the UrlHandler to set URL parameter with name and value Request::set($rx_matches[3], $request_part); } elseif (preg_match("/^(\\{)([a-zA-Z0-9_]+)(\\})\$/", $handle_part, $rx_matches)) { //the handle is a non regex URL variable //just set whatever is in the URL to the variable Request::set($rx_matches[2], $request_part); } elseif (!(preg_match("/^(rx)(\\{)(.*)(\\})\$/", $handle_part, $rx_matches) && preg_match("/" . $rx_matches[3] . "/", $request_part) || preg_match("/^(\\*+)\$/", $handle_part) || $request_part == $handle_part)) { //if all of te above fails, return false $handle_matched = false; } ++$i; } if (!$handle_matched) { continue; } //set the route on the UrlHandler Request::route($this); return true; } } return false; }
<compile value="change+version" />'); } }, 'new:view'); /** * Creates a new view which extends a layout */ Workbench::register('^new:view ([a-zA-Z0-9\\_\\.]+) ([a-zA-Z0-9\\_\\.]+)$', function ($argv) { $name = $argv[1]; $dot_pos = strrpos($name, '.'); // get and check layout $layout = $argv[2]; if (!Core\App::layouts()->file($layout . '.wow.php', 2)->exists()) { die('Layout does not exist'); } if ($dot_pos !== false) { $dir = Core\App::views()->directory(substr($name, 0, $dot_pos)); if (!$dir->exists()) { $dir->create(); } } $view = Core\App::views()->file($name . '.wow.php', 2); if (Core\Wow::flavor() == Core\Wow::AT_SIGN) { $view->write('@version:1 @layout:' . $layout . ' @compile:change+version'); } else { $view->write('<version value="1" /> <layout value="' . $layout . '" /> <compile value="change+version" />'); } });
} else { die("Configuration key not found"); } }); /** * Sets a configuration value */ Workbench::register('^config ([a-zA-Z]+) (.*)$', function ($argv) { $key = $argv[1]; $value = $argv[2]; $conf = Core\App::config()->file('App.configuration.json'); $configuration = json_decode($conf->read(), true); if (isset($configuration[$key])) { if ($value === 'true' || $value === 'false') { $value = boolval($value); } elseif (is_numeric($value)) { $value = intval($value); } $configuration[$key] = $value; $conf->write(json_encode($configuration)); // if environment changes , update db.yml if ($key == 'environment') { $db_file = Core\App::config()->file('ORM.db.yml'); $db = Spyc::YAMLLoad((string) $db_file); $db['environments']['default_database'] = $value; $db_file->write(Spyc::YAMLDump($db, 4, false, true)); } } else { die("Configuration key not found"); } });
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 }'); });
<?php namespace Application; use xTend\Workbench\Workbench; /** * Sets the wow flavor */ Workbench::register('wow:flavor (HTML|AT_SIGN|COMBINED)', function ($argv) { $file = Core\App::config()->file('Wow.Flavor.php'); $file->write('<?php /** * Sets the current Wow flavor * and initializes the Wow engine */ namespace ' . Workbench::namespace(Workbench::get('application')) . '; use ' . Workbench::namespace(Workbench::get('application')) . '\\Core\\Wow; Wow::flavor(Wow::' . $argv[1] . '); Wow::start();'); }, 'wow:flavor');
<?php namespace Application; use Application\Core\App; use Application\Core\FileHandler; App::configuration(json_decode(FileHandler::system('Config.App.configuration.json')->read(), true)); App::configuration(json_decode(FileHandler::system('Config.App.directories.json')->read(), true));
namespace Application; use xTend\Workbench\Workbench; /** * Creates a new basic layout */ Workbench::register('^new:layout ([a-zA-Z0-9\\_\\.]+)$', function ($argv) { $name = $argv[1]; $dot_pos = strrpos($name, '.'); if ($dot_pos !== false) { $dir = Core\App::layouts()->directory(substr($name, 0, $dot_pos)); if (!$dir->exists()) { $dir->create(); } } $layout = Core\App::layouts()->file($name . '.wow.php', 2); if (Core\Wow::flavor() == Core\Wow::AT_SIGN) { $layout->write('<!DOCTYPE html> <html> <head> @section:head </head> <body> @section:body </body> </html>'); } else { $layout->write('<!DOCTYPE html> <html> <head> <section name="head" />
/** * 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; }
<?php namespace Application; use xTend\Workbench\Workbench; Workbench::register('^init$', function ($argv) { $configuration_file = Core\App::config()->file('Sessions.sessions.json'); $configuration = json_decode($configuration_file->read(), true); // set values $configuration['sessionName'] = sha1(random_bytes(8)); $configuration['initiatedKey'] = sha1(random_bytes(8)); $configuration['userAgentKey'] = sha1(random_bytes(8)); $configuration['salt'] = sha1(random_bytes(8)); $configuration['userSessionsKey'] = sha1(random_bytes(8)); $configuration['userCookiesKey'] = sha1(random_bytes(8)); // write configuration $configuration_file->write(json_encode($configuration)); }, 'init'); Workbench::register('^init show$', function ($argv) { $configuration_file = Core\App::config()->file('Sessions.sessions.json'); $configuration = json_decode($configuration_file->read(), true); echo "\n"; foreach ($configuration as $key => $value) { echo str_pad($key, 30) . $value . "\n"; } echo "\n"; }, 'init show');
/** * Gets the path * * @return string */ public static function path() { if (self::$_path === false) { $location = trim(App::location(), '/'); if ($location != '' && strrpos($location, '/') != strlen($location) - 1) { $location .= '/'; } $location = str_replace('/', '\\/', $location); $rx = '(?:(?:^(' . $location . ')index\\.php$)|(?:^(' . $location . ')index\\.php\\/))'; self::$_path = preg_replace('/' . $rx . '/', '$1$2', trim($_SERVER['REQUEST_URI'], '/')); } return self::$_path; }
/** * Executes the router * * @return boolean */ public static function start() { $request = Request::path(); //allow method spoofing $post = Request::$post; if (isset($post['_method'])) { Request::method($post['_method']); } //check home route if (isset(self::$_home) && self::$_home->match($request)) { self::$_home->execute(); return true; } //check any routes foreach (self::$_any as $handle => $route_obj) { if ($route_obj->match($request)) { $route_obj->execute(); return true; } } //check for method routes | POST or GET $relevant_requests; if (Request::method() == "POST") { $relevant_requests = self::$_post; } elseif (Request::method() == "GET") { $relevant_requests = self::$_get; } elseif (Request::method() == "PUT") { $relevant_requests = self::$_put; } elseif (Request::method() == "DELETE") { $relevant_requests = self::$_delete; } elseif (Request::method() == "PATCH") { $relevant_requests = self::$_patch; } elseif (Request::method() == "OPTIONS") { $relevant_requests = self::$_options; } //check the releavant requests foreach ($relevant_requests as $handle => $route_obj) { if ($route_obj->match($request)) { $route_obj->execute(); return true; } } //no routes have been executed here //check for error page if (isset(self::$_default)) { self::$_default->execute(); return true; } App::throw(0x194); return false; }
<?php /** * Register development routes which are available when * the development status is on */ namespace Application; use Application\Core\App; use Application\Core\Router; use Application\Core\HTMLHandler; use Application\Core\StatusCodeHandler; Router::restrict(function () { return App::environment() == 'dev' || App::environment() == 'development'; }, function () { Router::get('xtend/codes', function () { $table = HTMLHandler::createDocument()->createElement('table'); $codes = StatusCodeHandler::all(); foreach ($codes as $code) { $row = $table->createElement('tr'); $row->createElement('td')->addText($code->hex()); $row->createElement('td')->addText($code->name()); $row->createElement('td')->addText($code->readable()); } $table->write(true); }); });
$controller = Core\App::models()->file($name . '.php'); $controller->write('<?php namespace ' . Workbench::namespace(Workbench::get('application')) . '; class ' . $class_name . ' extends Blueprints\\Model { protected static $_table = \'' . str_replace('.', '_', $name) . '\'; protected static $_id_column = \'id\'; }'); }, 'new:model'); /** * Creates a new model which doesn't inherit the blueprint */ Workbench::register('^new:model ([a-zA-Z0-9\\_\\.]+) empty$', 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::models()->directory(substr($name, 0, $dot_pos)); if (!$dir->exists()) { $dir->create(); } $class_name = substr($name, $dot_pos + 1); } // create controller file $controller = Core\App::models()->file($name . '.php'); $controller->write('<?php namespace ' . Workbench::namespace(Workbench::get('application')) . '; class ' . $class_name . ' { }'); });
/** * Executes phinx command * * @param $argv array */ Workbench::register('^phinx', function ($argv) { if (count($argv) > 1) { $config_param = false; foreach ($argv as $arg) { if (substr($arg, 0, 16) == '--configuration=' || $arg == '-c' || $arg == '--configuration') { $config_param = true; break; } } if ($config_param === false) { $argv[] = '-c'; $argv[] = '' . Core\App::config()->file('ORM.db.yml'); } } // create directory and exclude file $directory = Core\App::config()->directory('ORM.db'); if (!$directory->exists()) { $directory->create(); } $exclude_file = Core\App::config()->file('ORM.db..exclude', 1); if (!$exclude_file->exists()) { $exclude_file->write(""); } $_SERVER['argv'] = $argv; require __DIR__ . '/../Phinx/robmorgan-phinx/bin/phinx'; });
/** * Gets, sets or removes meta data * set value to null and unset to true to * remove a value from the meta file * * @param mixed $key * @param mixed $value * * @return mixed */ public function meta($key, $value = null, $unset = false) { if ($this->exists()) { $m_file = App::meta()->file(hash('sha256', $this->_path) . '.meta'); $meta = []; if ($m_file->exists()) { $meta = json_decode($m_file->read(), true); } if ($value === null && isset($meta[$key])) { return $meta[$key]; } elseif ($value === null && $unset === true) { unset($meta[$key]); } else { $meta[$key] = $value; } return $m_file->write(json_encode($meta)); } return false; }
<?php use application\core\App; /////////////////// //VIEWS REGISTRY // /////////////////// App::$views = array('index-view', 'map-view', 'header-view', 'details-view');