/** * */ public function initialize() { // Load boot $boot = $this->loadBoot(); // Build attributes $this->_cache = $boot['cache']; $this->_dependencies = $boot['dependencies']; $this->_environment = $boot['environment']; // Fix dependencies $this->_dependencies = $this->fixDependencies($this->_dependencies); // Are we in CLI mode? if (\z\app()->isCli() === true) { // Grab environment $request = new \fbenard\Zero\Classes\Request(); $environment = $request->argument('env'); // Store environment if (empty($environment) === false) { $this->_environment = $environment; } } else { if (array_key_exists('hosts', $boot) === true && array_key_exists($_SERVER['SERVER_NAME'], $boot['hosts']) === true) { // Grab the host $host = $boot['hosts'][$_SERVER['SERVER_NAME']]; // Grab environment if (array_key_exists('environment', $host) === true) { $this->_environment = $host['environment']; } } } }
/** * */ public function __construct() { // Build attributes $this->_argument = []; $this->_env = $_ENV; $this->_file = $_FILES; $this->_get = $_GET; $this->_header = []; $this->_post = $_POST; $this->_server = $_SERVER; // Inject HTTP headers and CLI options if (\z\app()->isCli() === true) { // Extract arguments if (array_key_exists('argv', $GLOBALS) === true && is_array($GLOBALS['argv']) === true) { // Parse each argument foreach ($GLOBALS['argv'] as $arg) { // Try to find the pattern --arg="value" $pattern = '/^\\-\\-([a-z]*)=(.*)$/'; if (preg_match($pattern, $arg, $matches) !== 1) { continue; } // Store the argument $this->_argument[$matches[1]] = $matches[2]; } } } else { $this->_header = apache_request_headers(); } }
/** * */ public function buildLogger($channel, $handlers = null) { // Create a new logger $logger = new \Monolog\Logger($channel); // Setup processors $logger->pushProcessor(new \Monolog\Processor\IntrospectionProcessor()); $logger->pushProcessor(new \Monolog\Processor\ProcessIdProcessor()); $logger->pushProcessor(new \Monolog\Processor\WebProcessor()); // Are we in CLI/verbose mode? if (\z\app()->isCli() === true) { // Define the level if (\z\app()->isVerbose() === true) { $level = \Monolog\Logger::DEBUG; } else { $level = \Monolog\Logger::INFO; } // Redirect to standard output $handler = new \Monolog\Handler\StreamHandler('php://stdout', $level); // Format for CLI $handler->setFormatter(new \fbenard\Zero\Services\Formatters\CliLogFormatter()); } else { // Redirect to /dev/null $handler = new \Monolog\Handler\NullHandler(); } // Push the handler $logger->pushHandler($handler); // Parse each handler if (is_array($handlers) === true) { foreach ($handlers as $handler) { // Push the handler $logger->pushHandler($handler); } } return $logger; }
/** * */ public function check() { // If the app is not in CLI mode // Then permission is not granted if (\z\app()->isCli() === false) { \z\e(EXCEPTION_PERMISSION_NOT_GRANTED); } }
/** * */ public function reset() { // No session in CLI if (\z\app()->isCli() === true) { return; } // Destroy the session session_unset(); session_destroy(); // Destroy the cookie setcookie('sessionKey', ''); }
<?php // Display all errors ini_set('display_errors', 1); error_reporting(E_ALL | E_STRICT); // Build paths define('PATH_ROOT', getcwd()); define('PATH_APP', PATH_ROOT . '/App'); define('PATH_COMPONENTS', PATH_ROOT . '/Components'); define('PATH_ZERO', PATH_COMPONENTS . '/fbenard/zero/App'); // Dependencies $dependencies = [PATH_COMPONENTS . '/autoload.php', PATH_ZERO . '/Core/zero-shortcuts.php']; foreach ($dependencies as $dependency) { if (file_exists($dependency) === false) { print "Cannot find dependency.\n"; trigger_error(null, E_USER_ERROR); } require_once $dependency; } // Setup error/exception handlers set_error_handler('\\fbenard\\Zero\\Services\\Managers\\ErrorManager::onError', E_ALL | E_STRICT); set_exception_handler('\\fbenard\\Zero\\Services\\Managers\\ExceptionManager::onException'); // Start the application \z\app()->run();
/** * */ public function setRoute($uri = null, $verb = null) { // Build the URI if (empty($uri) === true) { if (\z\app()->isCli() === true) { if (array_key_exists(1, $GLOBALS['argv']) === true) { $this->_uri = $GLOBALS['argv'][1]; } } else { $this->_uri = explode('?', \z\request()->server('REQUEST_URI'), 2)[0]; } } // Build the verb if (empty($verb) === true) { if (\z\app()->isCli() === true) { $this->_verb = 'CLI'; } else { $this->_verb = \z\request()->server('REQUEST_METHOD'); } } // Try to find the URI/verb in definitions foreach ($this->_definitions as $uri => $definition) { // Is the verb supported? if (array_key_exists($this->_verb, $definition['verbs']) === false) { continue; } // Get the verb $verb = $definition['verbs'][$this->_verb]; // Replace URI arguments by their pattern $uriFragments = explode('/', $uri); foreach ($uriFragments as $key => &$uriFragment) { if (preg_match('/\\{([a-zA-Z0-9]+)\\}/', $uriFragment, $matches) === 1 && array_key_exists($matches[1], $definition['arguments']) === true) { $uriFragment = '(' . $definition['arguments'][$matches[1]]['pattern'] . ')'; } } // Does the URI match? $pattern = '/^' . str_replace('/', '\\/', implode('/', $uriFragments)) . '\\/?$/'; if (preg_match($pattern, $this->_uri, $matches) !== 1) { continue; } // Remove the first match array_shift($matches); // Arguments are remaining matches $arguments = $matches; // Count arguments $nbArguments = count($arguments); $nbArgumentsDefined = count($definition['arguments']); // Store arguments into definition if ($nbArguments > 0 && $nbArguments === $nbArgumentsDefined) { $keys = array_keys($definition['arguments']); foreach ($arguments as $key => $value) { $definition['arguments'][$keys[$key]]['value'] = $value; } } // This is the route! $this->_route = array_merge($verb, ['arguments' => $definition['arguments'], 'post' => $definition['post'], 'pre' => $definition['pre']]); break; } // Do we have a route? if (is_null($this->_route) === true) { // Build definitions $definitions = []; foreach ($this->_definitions as $definitionCode => $definition) { $definitions[$definitionCode] = ['verbs' => array_keys($definition['verbs'])]; } // Throw the exception \z\e(EXCEPTION_ROUTE_NOT_FOUND, ['uri' => $this->_uri, 'verb' => $this->_verb, 'definitions' => $definitions]); } }
/** * */ function service($serviceCode = null, $clone = false) { if (empty($serviceCode) === true) { return \z\app()->serviceManager; } else { return \z\app()->serviceManager->getService($serviceCode, $clone); } }
/** * */ public function quit() { // Exit the process // Do not exit if app is embedded if (\z\app()->isEmbedded() === true) { return; } else { exit; } }
/** * */ public function redirect($url) { header('Location: ' . $url); \z\app()->quit(); }