Ejemplo n.º 1
0
 /**
  * Constructor
  *
  * @access public
  */
 public function __construct()
 {
     $aClass = explode('\\', get_called_class());
     $sClassName = $aClass[count($aClass) - 1];
     $sNamespaceName = preg_replace('/\\\\' . $sClassName . '$/', '', get_called_class());
     if (isset($sClassName)) {
         $sNamespaceBaseName = str_replace('\\Controller', '', $sNamespaceName);
         $sDefaultModel = $sNamespaceBaseName . '\\Model\\' . $sClassName;
         $sDefaultView = str_replace('\\', DIRECTORY_SEPARATOR, str_replace('Venus\\', '\\', $sNamespaceBaseName)) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . $sClassName . '.tpl';
         $sDefaultLayout = str_replace('\\', DIRECTORY_SEPARATOR, str_replace('Venus\\', '\\', $sNamespaceBaseName)) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . 'Layout.tpl';
         $this->model = function () use($sDefaultModel) {
             return new $sDefaultModel();
         };
         $this->view = function () use($sDefaultView) {
             return Vendor::getVendor('Apollina\\Template', $sDefaultView);
         };
         $this->layout = function () use($sDefaultLayout) {
             return Vendor::getVendor('Apollina\\Template', $sDefaultLayout, true);
         };
         $this->layout->assign('model', $sDefaultView);
     }
     $this->form = function () {
         return new Form();
     };
     $this->security = function () {
         return new Security();
     };
     $this->router = function () {
         return new Router();
     };
     $this->mail = function () {
         return new Mail();
     };
     $this->session = function () {
         return new Session();
     };
     $this->translator = function () {
         return new I18n();
     };
     $this->url = function () {
         return new UrlManager();
     };
     $this->cookie = function () {
         return new Cookie();
     };
     $this->di = function () {
         return new Di();
     };
     $this->request = function () {
         return new Request();
     };
     /**
      * Trigger on a model to initialize it. You could fill entity with it.
      */
     if (method_exists(get_called_class(), 'initialize')) {
         if (!isset(self::$_aInitialize[get_called_class()])) {
             static::initialize();
             self::$_aInitialize[get_called_class()] = true;
         }
     }
     /**
      * Trigger on a model to initialize it every time you construct it
      */
     if (method_exists(get_called_class(), 'onConstruct')) {
         static::onConstruct();
     }
 }
Ejemplo n.º 2
0
 /**
  * load a route
  *
  * @access private
  * @param  \stdClass $oRoute one route
  * @param  string $RequestUri URI
  * @return void
  */
 private function _route(\stdClass $oRoute, string $RequestUri)
 {
     $sCharset = 'UTF-8';
     if (isset($oRoute->route)) {
         $sRoute = str_replace("*", ".*", $oRoute->route);
         $sFinalRoute = preg_replace_callback('|\\[/{0,1}:([a-zA-Z_]+)\\]|', function ($aMatches) use($oRoute) {
             return "/{0,1}(?P<" . $aMatches[1] . ">" . $oRoute->constraints->{$aMatches[1]} . ")";
         }, $sRoute);
     } else {
         $sFinalRoute = '.*';
     }
     $RequestUri = preg_replace('/^([^?]+)\\?.*$/', '$1', $RequestUri);
     $RequestUri = preg_replace('#^' . $this->_sBaseUri . '#', '', $RequestUri);
     if (preg_match('#^' . $sFinalRoute . '$#', $RequestUri, $aMatch)) {
         if (isset($oRoute->location)) {
             $aParamEntries = array();
             foreach ($oRoute->constraints as $sName => $sType) {
                 if (isset($aMatch[$sName])) {
                     $aParamEntries[$sName] = $aMatch[$sName];
                 }
             }
             $oUrlManager = new UrlManager();
             header('Status: 301 Moved Permanently', false, 301);
             header('Location: ' . $oUrlManager->getUrl($oRoute->location, $aParamEntries));
             exit;
         }
         $this->_oSecurity = new Security();
         if (!$this->_oSecurity->checkSecurity() !== null) {
             return 403;
         }
         // create the $_GET by the URL
         foreach ($aMatch as $mKey => $sResults) {
             if (is_string($mKey)) {
                 $_GET[$mKey] = $sResults;
             }
         }
         if (isset($oRoute->methods) && $oRoute->methods != $_SERVER['REQUEST_METHOD']) {
             return false;
         }
         if (isset($oRoute->schemes) && $oRoute->schemes == 'https' && !Request::isHttpsRequest()) {
             return false;
         }
         if (isset($oRoute->cache) && isset($oRoute->cache->max_age) && !isset($_GET['flush'])) {
             $oMobileDetect = new \Mobile_Detect();
             if ($oMobileDetect->isMobile()) {
                 $sCacheExt = '.mobi';
             } else {
                 $sCacheExt = '';
             }
             $mCacheReturn = Cache::get($RequestUri . $sCacheExt, $oRoute->cache->max_age);
             if ($mCacheReturn && count($_POST) < 1) {
                 echo $mCacheReturn;
                 return true;
             }
         }
         if (isset($oRoute->cache)) {
             $this->_checkCache($oRoute->cache);
         }
         if (isset($oRoute->controller)) {
             define('PORTAL', preg_replace('/^\\\\Venus\\\\src\\\\([a-zA-Z0-9_]+)\\\\.+$/', '$1', $oRoute->controller));
             set_include_path(get_include_path() . PATH_SEPARATOR . 'src' . PATH_SEPARATOR . PORTAL . PATH_SEPARATOR . 'public');
             if (isset($oRoute->content_type)) {
                 if ($oRoute->content_type == 'json') {
                     header('Content-type: application/json; charset=' . $sCharset . '');
                 } else {
                     if ($oRoute->content_type == 'html') {
                         header('Content-type: text/html; charset=' . $sCharset . '');
                     } else {
                         if ($oRoute->content_type == 'jpeg') {
                             header('Content-type: image/jpeg');
                         }
                     }
                 }
             } else {
                 header('Content-type: text/html; charset=' . $sCharset . '');
             }
             $sControllerName = $oRoute->controller;
             $sActionName = $oRoute->action;
             $oController = new $sControllerName();
             $aEntries = array();
             if (isset($oRoute->constraints) && is_object($oRoute->constraints)) {
                 $mReturn = null;
                 foreach ($oRoute->constraints as $sName => $sType) {
                     if (isset($_GET[$sName]) && $_GET[$sName] != '') {
                         $aEntries[] = $_GET[$sName];
                     } else {
                         if (isset($oRoute->defaults_constraints) && is_object($oRoute->defaults_constraints) && isset($oRoute->defaults_constraints->{$sName})) {
                             $aEntries[] = $oRoute->defaults_constraints->{$sName};
                         } else {
                             if (isset($_GET[$sName])) {
                                 $aEntries[] = $_GET[$sName];
                             } else {
                                 if (preg_match('/' . $sType . '/', '')) {
                                     $aEntries[] = '';
                                 } else {
                                     $this->_oLogger->warning('Error: Parameter ' . $sName . ' not exists!');
                                     break;
                                 }
                             }
                         }
                     }
                 }
                 if ($mReturn === null) {
                     $mReturn = $this->_loadController($oController, $sActionName, $aEntries);
                 }
             } else {
                 $mReturn = $this->_loadController($oController, $sActionName, $aEntries);
             }
             if (isset($oRoute->content_type)) {
                 if ($oRoute->content_type === 'json') {
                     $mReturn = json_encode($mReturn, JSON_PRETTY_PRINT);
                 }
             }
         } else {
             if (isset($oRoute->template) && isset($oRoute->layout) && $oRoute->layout === true) {
                 define('PORTAL', preg_replace('/^\\\\Venus\\\\src\\\\([a-zA-Z0-9_]+)\\\\.+$/', '$1', $oRoute->template));
                 set_include_path(get_include_path() . PATH_SEPARATOR . 'src' . PATH_SEPARATOR . PORTAL . PATH_SEPARATOR . 'public');
                 $oLayout = Vendor::getVendor('Apollina\\Template', DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . PORTAL . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . 'Layout.tpl');
                 if (isset($oRoute->vars)) {
                     foreach ($oRoute->vars as $sKey => $mValue) {
                         $oLayout->assign($sKey, $mValue);
                     }
                 }
                 $mReturn = $oLayout->assign('model', DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . PORTAL . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . $oRoute->template . '.tpl')->fetch();
             } else {
                 if (isset($oRoute->template)) {
                     define('PORTAL', preg_replace('/^\\\\Venus\\\\src\\\\([a-zA-Z0-9_]+)\\\\.+$/', '$1', $oRoute->template));
                     set_include_path(get_include_path() . PATH_SEPARATOR . 'src' . PATH_SEPARATOR . PORTAL . PATH_SEPARATOR . 'public');
                     $oTemplate = Vendor::getVendor('Apollina\\Template', DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . PORTAL . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . $oRoute->template . '.tpl');
                     if (isset($oRoute->vars)) {
                         foreach ($oRoute->vars as $sKey => $mValue) {
                             $oTemplate->assign($sKey, $mValue);
                         }
                     }
                     $mReturn = $oTemplate->fetch();
                 }
             }
         }
         // management of return or cache of it
         if (isset($oRoute->cache) && isset($oRoute->cache->max_age) && $mReturn) {
             $oMobileDetect = new \Mobile_Detect();
             if ($oMobileDetect->isMobile()) {
                 $sCacheExt = '.mobi';
             } else {
                 $sCacheExt = '';
             }
             if (defined('COMPRESS_HTML') && COMPRESS_HTML) {
                 $mReturn = str_replace(array("\t", "\r", "  "), array("", "", " "), $mReturn);
             }
             Cache::set($RequestUri . $sCacheExt, $mReturn, $oRoute->cache->max_age);
         }
         if ($mReturn) {
             echo $mReturn;
             return true;
         }
     }
 }