示例#1
0
 public static function compress($action = '')
 {
     // merge framework files
     $destFile = Registry::get('applicationPath') . '/cache/nf.all.php';
     if (is_file($destFile)) {
         unlink($destFile);
     }
     // get the actual folder of Nf in the app's settings
     $includedFiles = get_included_files();
     $folder = null;
     foreach ($includedFiles as $includedFile) {
         if (preg_match('%Nf\\/Autoloader\\.php$%', $includedFile, $regs)) {
             $folder = str_replace('/Autoloader.php', '', $includedFile);
             $allFiles = self::getAllFiles($folder);
             // sort by depth for include
             uasort($allFiles, array('self', 'orderFilesByDepth'));
             $bigInclude = '<?' . 'php' . "\n";
             foreach ($allFiles as $file) {
                 if (substr($file, -4) == '.php') {
                     $bigInclude .= "\n" . str_replace('<?' . 'php', '', file_get_contents($file));
                 }
             }
             file_put_contents($destFile, $bigInclude);
             // merge routes files
             $destRoutesFile = Registry::get('applicationPath') . '/cache/routes.all.php';
             if (is_file($destRoutesFile)) {
                 unlink($destRoutesFile);
             }
             $router = \Nf\Router::getInstance();
             $router->setRootRoutes();
             $router->setRoutesFromFiles();
             $router->addAllRoutes();
             $allRoutes = $router->getAllRoutes();
             $bigInclude = '<?' . 'php' . "\n return ";
             $bigInclude .= var_export($allRoutes, true);
             $bigInclude .= ";";
             file_put_contents($destRoutesFile, $bigInclude);
             break;
         }
     }
     if ($folder === null) {
         die('The cache already exists, remove the generated files before in /cache (nf.all.php and routes.all.php)' . PHP_EOL);
     }
 }
示例#2
0
 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();
     }
 }
示例#3
0
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'));