public function open() { if (empty($this->file)) { $path = \Painless::app()->env(\Painless::APP_PATH) . 'log.txt'; $this->file = fopen($path, 'a'); } }
public function get() { // See what help is being requested $topic = $this->request->getParam(0); if (empty($topic)) { $topic = 'help'; } // Check if the topic is supported $model = \Painless::app()->load('model/help/topic'); $response = $model->getTopic($topic); // Do nothing because there's no logic to handle. Let view handle it. $this->response($response); }
/** * Retrieves a topic from the help topic template directory * - 200 -> topic found * - 404 -> topic not found * @param string $topic the name of the topic to search for * @return PainlessResponse a response object detailing the status of the operation */ public function getTopic($topic) { // Try to get the topic try { ob_start(); \Painless::app()->load("tpl/help/{$topic}", LP_DEF_ONLY); $topic = ob_get_flush(); } catch (\ErrorException $e) { // Return a topic not found return $this->response(404, $topic . ' not found'); } return $this->response(200, 'Found', array('help' => $topic)); }
/** * Adds a new config key into morphine * @return void */ public function addConfig($key, $value) { // Get the config DAO $dao = \Painless::app()->load('dao/config/config/sqlite'); // Set the values $dao->key = $key; $dao->value = $value; $id = $dao->new(); if (empty($id)) { return $this->response(400, "Unable to create new config key-value item for `{$key}` (value = {$value})"); } return $this->response(200, 'OK', array('id' => $id)); }
/** * Redirects either to an external resource or to an internal workflow * @param string $path the path to redirect to */ protected function redirect($path) { $path = strtolower($path); // check if this is an external path if (stripos($path, 'http://') === FALSE && stripos($path, 'https://') === FALSE) { $path = \Painless::app()->env(\Painless::APP_URL) . $path; } // rebuild the response $response =& $this->response; $response->status = 302; $response->message = "Redirect"; $response->set(self::PATH, $path); }
protected function init() { // Localize all external variables $core = \Painless::app(); $appPath = $core->env(\Painless::APP_PATH); $appName = $core->env(\Painless::APP_NAME); $configPath = ''; $profile = ''; $aclPath = ''; // first, check if a deployment profile is issued $profile = $core->env(\Painless::PROFILE); // get the engine's implementor path if possible if (!empty($appPath)) { $configPath = $appPath . 'config/' . $appName; $aclPath = $appPath . 'config/' . $appName; $routesPath = $appPath . 'config/' . $appName; if ($profile) { $configPath .= '.' . $profile; } $configPath .= '.php'; $aclPath .= '.acl.php'; $routesPath .= '.route.php'; } // check if the config path is correct if (file_exists($configPath)) { require_once $configPath; if (!isset($config)) { throw new \ErrorException('Unable to find the config array in [' . $configPath . ']'); } $this->config = $config; // clean up because $config is going to be recycled after this unset($config); } else { throw new \ErrorException('Invalid config file [' . $configPath . ']'); } // load the acl array too if (file_exists($aclPath)) { require_once $aclPath; if (isset($config)) { $this->config = array_merge($this->config, $config); unset($config); } } // load the routes array too if (file_exists($routesPath)) { require_once $routesPath; if (isset($config)) { $this->config = array_merge($this->config, $config); } } }
public function get() { # GET execute/model/[module name]/[model name]/[params ...] // Get the model's module and name $module = $this->request->getParam('name'); $model = $this->request->getParam('name'); $params = $this->request->getParam('params'); // Get the model $model = \Painless::app()->load("model/{$module}/{$model}"); if (empty($model)) { return $this->response(404, 'Model not found'); } // Get the parameters }
public function open($options = array()) { // do not proceed if memcached is not available if (!extension_loaded('memcached')) { throw new \ErrorException('Memcached is not installed in your system.'); } $this->params = $options; // as usual, get the options from the config if not specified if (empty($this->params)) { $config = \Painless::app()->load('system/common/config'); $this->params = $config->get('memcached.*'); } // open a connection $host = array_get($this->params, 'memcached.host', FALSE); $port = (int) array_get($this->params, 'memcached.port', FALSE); $timeout = (int) array_get($this->params, 'memcached.timeout', FALSE); $this->conn = new Memcache(); return $this->conn->connect($host, $port, $timeout); }
/** * Edits a config key in morphine * @return void */ public function post() { // Get the config model $model = \Painless::app()->load('model/config/manager'); // Get the key to update and the value $key = $this->request->getParam('key'); $value = $this->request->getParam('value'); // Try to update the config key $response = $model->updateConfig($key, $value); // Handle the return status if ($response->status === 200) { $this->response(200, 'OK'); return; } else { $this->response($response); return; } }
public function __construct() { // Initialize the namespace $this->namespace = \Painless::app()->env(\Painless::APP_NAME); }
public function dispatch() { // Check and load the router $router = \Painless::app()->load('system/common/router'); // Process the command line arguments into a proper URI $uri = $this->processArgs(); try { // Let the router process the business logic $response = $router->process($uri); } catch (PainlessWorkflowNotFoundException $e) { // Construct a 404 response $response = \Painless::app()->load('system/workflow/response', LP_LOAD_NEW); $response->status = 404; $response->message = 'Unable to locate workflow'; } catch (\ErrorException $e) { $response = \Painless::app()->load('system/workflow/response', LP_LOAD_NEW); $response->status = 500; $response->message = $e->getMessage(); } // Pass the control to the renderer $render = \Painless::app()->load('system/common/render'); $output = $render->process($response); return $output; }
protected function processCli($method, &$uri, $data = array(), $roles = array()) { // Localize the variables $module = ''; $controller = ''; $param = array(); // If it's an APP call, a URI must be given if (empty($uri)) { return FALSE; } // Check if argv is set in the Core (which should be the case in the // bootstrap process if writing for a CLI app) $argv = \Painless::app()->env(\Painless::CLI_ARGV); if (!empty($arv)) { // TODO: Finish this } // Dispense the request object $request = \Painless::manufacture('request', $method, $module, $controller, $param, '', PHP_SAPI); // If the method is GET or POST, merge the arrays into params if ($method === \Painless\System\Workflow\Request::GET) { $request->params($_GET, TRUE, Request::SRC_ALL, Request::PS_ASSOC); } elseif ($method === \Painless\System\Workflow\Request::POST || $method === \Painless\System\Workflow\Request::PUT) { $request->params($_POST, TRUE, Request::SRC_ALL, Request::PS_ASSOC); } return $request; }
protected function handle500($request, $response) { $response->set(self::TPL_PATH, \Painless::app()->env(\Painless::APP_PATH) . 'view/error-500.tpl'); return $response; }
/** * Loads a component * @param string $ns the component's namespace * @param int $opt loading parameters * @return mixed returned value depends on the loading parameters */ public function load($ns, $opt = \Painless::LP_ALL) { // Localize the Core instance for multiple access $app = \Painless::app(); if (!empty($app)) { // If LP_LOAD_NEW is not defined, try to see if the component has already // been cached and return that instead if so $com = $app->com($ns); if (!empty($ns) && !empty($com) && !($opt & \Painless::LP_SKIP_CACHE)) { return $com; } } // Explode the namespace string into an array to make it easier to work // with $nsa = explode('/', $ns); if (empty($nsa) || count($nsa) <= 1) { throw new \ErrorException('Namespace cannot be NULL or a malformed format [' . $ns . ']'); } // The component type uses a dash convention, thus the need for this conversion $comType = dash_to_camel($nsa[0]); // Grab the load information from the respective component type handler $meta = $this->{$comType}($nsa, $ns); // Declare the variables and constants to use as a good engineering // practice :) $comBase = NULL; $comExt = NULL; $isDefCore = (bool) ($opt & \Painless::LP_DEF_BASE); $isDefExt = (bool) ($opt & \Painless::LP_DEF_APP); $isCacheCore = (bool) ($opt & \Painless::LP_CACHE_BASE); $isCacheExt = (bool) ($opt & \Painless::LP_CACHE_APP); $isRetCore = (bool) ($opt & \Painless::LP_RET_BASE); $isRetExt = (bool) ($opt & \Painless::LP_RET_APP); // Load the definition of the base class, if possible if (FALSE !== $meta['basepath'] && file_exists($meta['basepath']) && $isDefCore && !class_exists($meta['basename'], FALSE)) { require_once $meta['basepath']; } // Instantiate the core class if (class_exists($meta['basename'], FALSE) && ($isRetCore || $isCacheCore)) { $comBase = new $meta['basename'](); // If caching is required (by enabling the LP_CACHE_CORE flag), save // the instantiated object into Painless's central cache if ($isCacheCore && !empty($app)) { $app->com($ns, $comBase); } } // Load the definition of the ext class, if possible if (file_exists($meta['extpath']) && $isDefExt && !class_exists($meta['extname'], FALSE)) { require_once $meta['extpath']; } // Instantiate the ext class if (class_exists($meta['extname'], FALSE) && ($isRetExt || $isCacheExt)) { $comExt = new $meta['extname'](); // If caching is required (by enabling the LP_CACHE_EXT flag), save // the instantiated object into Painless's central cache (overwrite // LP_CACHE_CORE if possible if ($isCacheExt && !empty($app)) { $app->com($ns, $comExt); } } // Now that we have done all the loading bit, figure out what to return. // If both LP_RET_CORE and LP_RET_EXT are not set, then no values are // expected to be returned at all if (!($isRetCore && $isRetExt)) { return NULL; } elseif ($comExt && $isRetExt) { return $comExt; } elseif (empty($comExt) && $comBase && $isRetCore) { return $comBase; } // Don't know what to return, so we'll be nice and return an exception // instead. :) //throw new LoaderException( 'Unable to load the component [' . $ns . ']' ); return NULL; }
/** * Initializes an application * @param string $appName the name of the application (dash-delimited) * @param string $appPath the path of the application (dash-delimited) * @param boolean $useExtLoader set to TRUE to have the loader check for the * existence of an extended loader inside the * app's extensions, or FALSE to save time and * cycles */ public static function initApp($appName, $appPath, $useExtLoader = TRUE) { // Append a backslash to $implPath if none is provided $appPath[strlen($appPath) - 1] !== '/' and $appPath .= '/'; // Instantiate the Core. Here's the thing - both Core (which contains // instances of components, environment variables, etc) and Loader (which // handles loading of components) can be extended by the App, and thus // we will need to do some creative loading here. // // First, check if there's an extended version of a loader inside the // app's extensions. If there is (and $useExtLoader is set to TRUE), // instantiate that and use it to load the Core. Then save the loader // into Core. $loaderPath = __DIR__ . '/system/common/loader' . EXT; require_once $loaderPath; $core = \Painless\System\Common\Loader::init($appName, $appPath, __DIR__ . '/', $useExtLoader); // Register the app \Painless::app($appName, $core); // Set the registered app as the active one static::$curr = $appName; // Register an autoloader spl_autoload_register('\\Painless\\System\\Common\\Loader::autoload'); return $core; }