public static function displayErrorHelper($err, $alternativeSeparator = false) { $output = ''; $separator = $alternativeSeparator ? static::NEWLINE_ALT : static::NEWLINE; if ($err['type'] != 'fatal') { $output .= static::colorText($err['type'] . ': ' . \Nf\Error\Handler::recursiveArrayToString(static::escape($err['message'])), 'red'); $output .= $separator; $output .= static::colorText($err['file'] . ' (line ' . $err['line'] . ')', 'green', $alternativeSeparator); $output .= $separator . '-----' . $separator; $output .= implode($separator, self::getFileSample($err['file'], $err['line'])); $output .= $separator . '-----' . $separator; $trace = $err['fullException']->getTrace(); foreach ($trace as $entry) { $output .= self::stackTracePrintEntry($entry); if (isset($entry['file']) && isset($entry['line'])) { $output .= '-----' . $separator; $output .= implode($separator, self::getFileSample($entry['file'], $entry['line'], 2)); $output .= $separator . '-----' . $separator; } } } else { $output .= $err['message'] . $separator; $output .= static::preFormatErrorText(0, $alternativeSeparator); $output .= self::stackTracePrintEntry($err, 2, $alternativeSeparator); $output .= static::preFormatErrorText(1, $alternativeSeparator); } return $output; }
error_reporting(E_ALL); /********************************************************* * Includes * *******************************************************/ $libraryPath = realpath(dirname(__FILE__) . '/../vendor/nofussframework/nofussframework/Nf'); $applicationPath = realpath(dirname(__FILE__) . '/..'); /********************************************************* * My application * ********************************************************/ $applicationNamespace = 'App'; /********************************************************* * Autoloader * *******************************************************/ $nfAllFile = $applicationPath . '/cache/Nf.all.php'; if (file_exists($nfAllFile)) { require $nfAllFile; } else { require $libraryPath . '/Autoloader.php'; } $autoloader = new \Nf\Autoloader(); $autoloader->addMap(); $autoloader->addNamespaceRoot('Nf', $libraryPath); $autoloader->addNamespaceRoot($applicationNamespace, $applicationPath . '/models'); $autoloader->register(); /******************************************************* */ $bootstrap = new \Nf\Bootstrap($libraryPath, $applicationPath); \Nf\Error\Handler::setErrorHandler(); $bootstrap->setApplicationNamespace($applicationNamespace); $bootstrap->go(); printf("\n%' 8d:%f", memory_get_peak_usage(true), microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);
function go() { if (php_sapi_name() == 'cli') { $inAction = $this->initCliEnvironment(); $uri = $inAction['uri']; Error\Handler::setErrorDisplaying(); $front = Front::getInstance(); $request = new Front\Request\Cli($uri); $front->setRequest($request); $request->setAdditionalCliParams(); $response = new Front\Response\Cli(); $front->setResponse($response); $front->setApplicationNamespace($this->_applicationNamespace); $this->setTimezone(); // routing $router = Router::getInstance(); $front->setRouter($router); $router->addAllRoutes(); // order in finding routes $router->setStructuredRoutes(); $front->addModuleDirectory($this->_applicationNamespace, Registry::get('applicationPath') . '/application/cli/'); $front->addModuleDirectory('library', Registry::get('libraryPath') . '/php/application/cli/'); $labelManager = LabelManager::getInstance(); $labelManager->loadLabels(Registry::get('locale')); $localization = Localization::getInstance(); $localization->setLocale(Registry::get('locale')); if ($inAction['type'] == 'default') { $testDispatch = $front->dispatch(); if ($testDispatch) { if ($front->init() !== false) { $front->launchAction(); $front->postLaunchAction(); } $response->sendResponse(); } else { throw new \Exception('Action not found : ' . $uri); } } else { $front->parseParameters($inAction['uri']); $className = array(); // $inAction['uri'] might be a class name with a static method like \Nf\Make::compress if (strpos($inAction['uri'], '\\') !== false) { if (strpos($inAction['uri'], '::') === false) { throw new \Exception('You have to specify the model and method to call, or just choose a method from the "Nf\\Make" class.'); } else { $uriSplit = explode('::', $inAction['uri']); $className = $uriSplit[0]; $methodName = $uriSplit[1]; $obj = new $className(); $className::$methodName(); } } else { // or an already integrated method in Nf\Make $methodName = $inAction['uri']; \Nf\Make::$methodName(); } } } else { $this->initHttpEnvironment(); Error\Handler::setErrorDisplaying(); $front = Front::getInstance(); $request = new Front\Request\Http(); $front->setRequest($request); $response = new Front\Response\Http(); $front->setResponse($response); $front->setApplicationNamespace($this->_applicationNamespace); $this->setTimezone(); // routing $router = Router::getInstance(); $front->setRouter($router); $router->addAllRoutes(); // order in finding routes $router->setRoutesFromFiles(); $router->setRootRoutes(); $router->setStructuredRoutes(); // modules directory for this version $front->addModuleDirectory($this->_applicationNamespace, Registry::get('applicationPath') . '/application/' . Registry::get('version') . '/'); $front->addModuleDirectory('library', Registry::get('libraryPath') . '/php/application/' . Registry::get('version') . '/'); $config = Registry::get('config'); if (isset($config->session->handler)) { $front->setSession(Session::start()); } $labelManager = LabelManager::getInstance(); $labelManager->loadLabels(Registry::get('locale')); $localization = Localization::getInstance(); Localization::setLocale(Registry::get('locale')); $testDispatch = $front->dispatch(); $requestIsClean = $request->sanitizeUri(); if ($requestIsClean) { if ($testDispatch === true) { $request->setPutFromRequest(); if (!$request->redirectForTrailingSlash()) { if ($front->init() !== false) { if (!$front->response->isRedirect()) { $front->launchAction(); } if (!$front->response->isRedirect()) { $front->postLaunchAction(); } } } } else { Error\Handler::handleNotFound(404); } } else { Error\Handler::handleForbidden(403); } $response->sendResponse(); } }
require $libraryPath . '/Nf/Autoloader.php'; $autoloader = new \Nf\Autoloader(); $autoloader->addNamespaceRoot($applicationNamespace, $applicationPath . '/models'); $autoloader->addNamespaceRoot('Nf', $libraryPath . '/Nf'); $autoloader->addNamespaceRoot('Library', $libraryPath . '/php/models'); $autoloader->addNamespaceRoot('', $applicationPath . '/models'); $autoloader->register(); \Nf\Registry::set('libraryPath', $libraryPath); \Nf\Registry::set('applicationPath', $applicationPath); $urlIni = \Nf\Ini::parse(\Nf\Registry::get('applicationPath') . '/configs/url.ini', true); \Nf\Registry::set('urlIni', $urlIni); \Nf\Registry::set('environment', 'test'); \Nf\Registry::set('locale', $urlIni->i18n->defaultLocale); \Nf\Registry::set('version', 'cli'); $config = \Nf\Ini::parse(\Nf\Registry::get('applicationPath') . '/configs/config.ini', true, \Nf\Registry::get('locale') . '_' . \Nf\Registry::get('environment') . '_' . \Nf\Registry::get('version')); \Nf\Registry::set('config', $config); \Nf\Error\Handler::setErrorDisplaying(); $front = \Nf\Front::getInstance(); $request = new \Nf\Front\Request\Cli('/'); $front->setRequest($request); $response = new \Nf\Front\Response\Cli(); $front->setResponse($response); $front->setApplicationNamespace($applicationNamespace); // routing $router = \Nf\Router::getInstance(); $front->setRouter($router); $router->addAllRoutes(); $labelManager = \Nf\LabelManager::getInstance(); $labelManager->loadLabels(\Nf\Registry::get('locale')); $localization = \Nf\Localization::getInstance(); $localization->setLocale(\Nf\Registry::get('locale'));
public function log($err) { $config = Registry::get('config'); // We need a transport - UDP via port 12201 is standard. $transport = new \Gelf\Transport\UdpTransport($config->error->logger->gelf->ip, $config->error->logger->gelf->port, \Gelf\Transport\UdpTransport::CHUNK_SIZE_LAN); // While the UDP transport is itself a publisher, we wrap it in a real Publisher for convenience // A publisher allows for message validation before transmission, and it calso supports to send messages // to multiple backends at once $publisher = new \Gelf\Publisher(); $publisher->addTransport($transport); $fullMessage = \Nf\Front\Response\Cli::displayErrorHelper($err); // Now we can create custom messages and publish them $message = new \Gelf\Message(); $message->setShortMessage(Handler::recursiveArrayToString($err['message']))->setLevel(\Psr\Log\LogLevel::ERROR)->setFile($err['file'])->setLine($err['line'])->setFullMessage($fullMessage); if (php_sapi_name() == 'cli') { global $argv; $message->setAdditional('url', 'su ' . $_SERVER['LOGNAME'] . ' -c "php ' . Registry::get('applicationPath') . '/html/' . implode(' ', $argv) . '"'); } else { $message->setAdditional('url', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); } if (isset($config->error->logger->additionals)) { foreach ($config->error->logger->additionals as $additionalName => $additionalValue) { $message->setAdditional($additionalName, $additionalValue); } } if ($publisher->publish($message)) { return true; } else { return false; } }