/** * Method to show the content. * * @return mixed * @throws \Exception If not supported request method or bad controller */ public function render() { if (Sapi::isCli() && Config::get('environment') == 'production') { $suffix = 'CliAction'; $action = $this->request->fromCli()->get('action', 'index'); } else { $suffix = 'Action'; if ($this->request->getMethod() != 'GET' && $this->request->getMethod() != 'POST') { $redirectUrl = new Value($this->env->REDIRECT_URL); $redirectUrl = $redirectUrl->deleteLeading('/')->deleteTrailing('/')->explode('/'); $action = isset($redirectUrl[1]) ? $redirectUrl[1] : 'index'; } else { $bag = sprintf('from%s', ucfirst(strtolower($this->request->getMethod()))); $action = $this->request->{$bag}()->get('action', 'index'); } if (Config::get('app.routeable') === true && $this->router instanceof \Pimf\Router) { $target = $this->router->find(); if ($target instanceof \Pimf\Route\Target) { $action = $target->getAction(); Request::$getData = new Param(array_merge($target->getParams(), Request::$getData->getAll())); } } } $action = strtolower($action) . $suffix; if (method_exists($this, 'init')) { call_user_func(array($this, 'init')); } if (!method_exists($this, $action)) { throw new Bomb("no action '{$action}' defined at controller " . get_class($this)); } return call_user_func(array($this, $action)); }
/** * @param Request $request * @param string $repositoryPath * @param string $prefix * @param \Pimf\Router $router * * @throws \Pimf\Resolver\Exception If no controller found at the repository path */ public function __construct(\Pimf\Request $request, $repositoryPath = '/Controller', $prefix = 'Pimf\\', $router) { $controllerName = $request->fromGet()->get('controller'); $this->router = $router; if (Config::get('app.routeable') === true) { $target = $this->router->find(); if ($target instanceof \Pimf\Route\Target) { $controllerName = $target->getController(); } } if (Sapi::isCli() && Config::get('environment') == 'production') { $controllerName = $request->fromCli()->get('controller'); } if (!$controllerName) { $controllerName = Config::get('app.default_controller'); } $this->repositoryPath = $repositoryPath; $this->request = $request; $this->controllerClass = $prefix . 'Controller\\'; $basepath = $this->repositoryPath . '/'; $controller = ucfirst($controllerName); if (Str::isEvilPath($basepath . $controller)) { throw new Bomb('directory traversal attack is not funny!'); } $this->controllerPath = $basepath . $controller . '.php'; if (!file_exists($this->controllerPath)) { throw new Bomb('no "' . $controller . '" controller found at the repository path'); } }
/** * @param string $template * @param array $data */ public function __construct($template, array $data = array()) { parent::__construct($template, $data); $conf = Config::get('view.haanga'); $options = ['debug' => $conf['debug'], 'template_dir' => $this->path, 'autoload' => $conf['auto_reload']]; if ($conf['cache'] === true) { $options['cache_dir'] = $this->path . '/haanga_cache'; } require_once BASE_PATH . "Haanga/lib/Haanga.php"; \Haanga::configure($options); }
/** * @param string $template * @param array $data */ public function __construct($template, array $data = array()) { parent::__construct($template, $data); $conf = Config::get('view.twig'); require_once BASE_PATH . "Twig/vendor/autoload.php"; $options = array('debug' => $conf['debug'], 'auto_reload' => $conf['auto_reload']); if ($conf['cache'] === true) { $options['cache'] = $this->path . '/twig_cache'; } // define the Twig environment. $this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array($this->path)), $options); }
/** * @param string $appClr * @param string $coreClr * @param string $root * * @return array */ public static function collect($appClr = null, $coreClr = null, $root = null) { $classes = array(); if (!$root && !$coreClr && !$appClr) { $coreClr = str_replace('/', DS, BASE_PATH . '/pimf-framework/core/Pimf/Controller/'); $appClr = str_replace('/', DS, BASE_PATH . '/app/' . Config::get('app.name') . '/Controller/'); } foreach (array($appClr, $coreClr) as $dir) { $iterator = new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)), '/^.+\\.php$/i', \RecursiveRegexIterator::GET_MATCH); foreach (iterator_to_array($iterator, false) as $file) { $file = str_replace("\\", '/', current($file)); $file = str_replace('/', DS, $file); $name = str_replace(array(BASE_PATH . DS . 'pimf-framework' . DS . 'core' . DS, BASE_PATH . DS . 'app' . DS), '', $file); $name = str_replace(DS, '\\', $name); $name = str_replace('.php', '', $name); $classes[] = '\\' . $name; } } return $classes; }
/** * @param int $code * @param string $status * @param boolean $exit */ protected static function view($code, $status, $exit = true) { if (Sapi::isCli()) { echo $status . PHP_EOL; if ($exit) { exit; } } self::send($code, $status); $appTpl = str_replace('/', DS, BASE_PATH . 'app/' . Config::get('app.name') . '/_error/' . $code . '.php'); $coreTpl = str_replace('/', DS, BASE_PATH . 'pimf-framework/core/Pimf/_error/' . $code . '.php'); $coreTpl = str_replace(DS . 'pimf-framework' . DS . 'pimf-framework' . DS, DS . 'pimf-framework' . DS, $coreTpl); if (file_exists($appTpl) && is_readable($appTpl)) { include $appTpl; if ($exit) { exit(1); } } include $coreTpl; if ($exit) { exit(1); } }
/** * Please bootstrap first, than run the application! * Run a application, let application accept a request, route the request, * dispatch to controller/action, render response and return response to client finally. * * @param array $get Array of variables passed to the current script via the URL parameters. * @param array $post Array of variables passed to the current script via the HTTP POST method. * @param array $cookie Array of variables passed to the current script via HTTP Cookies. * @param array $files An associative array FILES of items uploaded to the current script via the HTTP POST method. * * @return void */ public static function run(array $get, array $post, array $cookie, array $files) { $cli = array(); if (Sapi::isCli()) { $cli = Cli::parse((array) self::$env->argv); if (count($cli) < 1 || isset($cli['list'])) { Cli::absorb(); exit(0); } } $prefix = Str::ensureTrailing('\\', Config::get('app.name')); $repository = BASE_PATH . 'app/' . Config::get('app.name') . '/Controller'; if (isset($cli['controller']) && $cli['controller'] == 'core') { $prefix = 'Pimf\\'; $repository = BASE_PATH . 'pimf-framework/core/Pimf/Controller'; } $request = new Request($get, $post, $cookie, $cli, $files, self::$env); $resolver = new Resolver($request, $repository, $prefix, self::$router); $sessionized = Sapi::isWeb() && Config::get('session.storage') !== ''; if ($sessionized) { Session::load(); } $pimf = $resolver->process(self::$env, self::$em, self::$logger); if ($sessionized) { Session::save(); Cookie::send(); } $pimf->render(); }
/** * Magic Method for calling the methods on the default cache storage. * * @param $method * @param $parameters * * @return mixed */ public static function __callStatic($method, $parameters) { return call_user_func_array(array(static::storage(Config::get('cache.storage')), $method), $parameters); }
/** * Clean up expired sessions. * * @return void */ public function clean() { if ($this->storage instanceof \Pimf\Contracts\Cleanable) { $this->storage->clean(time() - Config::get('session.lifetime') * 60); } }
/** * @param \Exception $exception * @param Logger $logger */ public static function log(\Exception $exception, Logger $logger) { if (Config::get('error.log') === true) { $logger->error($exception->getMessage() . ' ' . $exception->getTraceAsString()); } }
<?php /* |-------------------------------------------------------------------------- | PIMF core bootstrap used for unit testing |-------------------------------------------------------------------------- */ if (!defined('DS')) { define('DS', DIRECTORY_SEPARATOR); } if (!defined('BASE_PATH')) { define('BASE_PATH', realpath(__DIR__) . DS); } require_once 'autoload.core.php'; require_once 'utils.php'; \Pimf\Config::load(array('environment' => 'testing', 'timezone' => 'UTC', 'ssl' => false, 'app' => array('name' => 'MyFirstBlog', 'key' => 'some5secret5key5here', 'default_controller' => 'blog', 'routeable' => true, 'restfull' => false, 'url' => 'http://localhost', 'index' => '', 'asset_url' => ''), 'production' => array('db' => array('driver' => 'sqlite', 'database' => 'app/MyFirstBlog/_database/blog-production.db')), 'testing' => array('db' => array('driver' => 'sqlite', 'database' => 'app/MyFirstBlog/_database/blog-production.db')), 'bootstrap' => array('local_temp_directory' => '/tmp/'), 'error' => array('ignore_levels' => array(0), 'debug_info' => true, 'log' => true), 'logging' => array('storage' => 'file'), 'session' => array('storage' => 'memory', 'storage_path' => 'app/MyFirstBlog/_session/', 'database' => array('driver' => 'sqlite', 'database' => 'app/MyFirstBlog/_session/blog-session.db'), 'garbage_collection' => array(2, 100), 'lifetime' => 60, 'expire_on_close' => false, 'cookie' => 'pimf_session', 'path' => '/', 'domain' => null, 'secure' => false), 'cache' => array('storage' => 'memory', 'storage_path' => 'app/MyFirstBlog/_cache/', 'database' => array('driver' => 'sqlite', 'database' => 'app/MyFirstBlog/_cache/blog-cache.db'), 'key' => 'pimfmaster', 'memcached' => array('servers' => array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100))))); $env = new \Pimf\Environment($_SERVER); $envData = $env->data(); \Pimf\Logger::setup($env->getIp(), $envData->get('PHP_SELF', $envData->get('SCRIPT_NAME'))); \Pimf\Util\Header\ResponseStatus::setup($envData->get('SERVER_PROTOCOL', 'HTTP/1.0')); \Pimf\Util\Header::setup($env->getUserAgent()); \Pimf\Url::setup($env->getUrl(), $env->isHttps()); \Pimf\Uri::setup($env->PATH_INFO, $env->REQUEST_URI); \Pimf\Util\Uuid::setup($env->getIp(), $env->getHost()); unset($env, $envData);
/** * @param string $type * @param string $for * * @return bool * @throws \DomainException */ protected function createTable($type, $for) { $type = trim($type); try { $pdo = $file = null; switch ($for) { case 'cache': $pdo = Factory::get(Config::get('cache.database')); $file = 'create-cache-table-' . $type . '.sql'; break; case 'session': $pdo = Factory::get(Config::get('session.database')); $file = 'create-session-table-' . $type . '.sql'; break; } $file = str_replace('/', DS, BASE_PATH . 'pimf-framework/core/Pimf/_database/' . $file); return $pdo->exec(file_get_contents(new File($file))) || print_r($pdo->errorInfo(), true); } catch (\PDOException $pdoe) { throw new Bomb($pdoe->getMessage()); } }
/** * Create a new session storage instance. * * @param string $storage * * @return Storage\Storage * @throws \RuntimeException */ public static function factory($storage) { switch ($storage) { case 'apc': return new Storage\Apc(Cache::storage('apc')); case 'cookie': return new Storage\Cookie(); case 'file': return new Storage\File(Config::get('session.storage_path')); case 'pdo': return new Storage\Pdo(Pdo\Factory::get(Config::get('session.database'))); case 'memcached': return new Storage\Memcached(Cache::storage('memcached')); case 'memory': return new Storage\Memory(); case 'redis': return new Storage\Redis(Cache::storage('redis')); case 'dba': return new Storage\Dba(Cache::storage('dba')); default: throw new \RuntimeException("Session storage [{$storage}] is not supported."); } }
/** * @param string $template * @param array $data * @param string $path Path to templates if you do not want to use PIMF framework restriction. */ public function __construct($template = 'default.phtml', array $data = array(), $path = null) { $this->data = new \ArrayObject($data, \ArrayObject::ARRAY_AS_PROPS); $this->path = !$path ? BASE_PATH . 'app/' . Config::get('app.name') . '/_templates' : $path; $this->template = (string) $template; }
/** * Get cleaner URLs or old-fashioned » RFC 3986 URL-query string. * * @param string $route controller/action * @param array $params * @param null $https * @param bool $asset * * @return string */ public static function compute($route = '', array $params = array(), $https = null, $asset = false) { // if your application should work with RFC 3986 URL-query strings if (Config::get('app.routeable') === false) { list($controller, $action) = explode('/', $route); $params = array_merge(compact('controller', 'action'), $params); return Str::ensureTrailing('/', self::format($https, $asset)) . '?' . http_build_query($params, null, '&'); } // otherwise PIMF will serve you cleaner URLs $slug = implode('/', $params); if ($slug != '') { $slug = '/' . $slug; } return self::to($route, $https, $asset) . $slug; }