/** * */ 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]); } }
/** * */ public function setLocale($localeCode) { // Store the locale in session \z\request()->session('culture/locale', $localeCode); // Re-initialize the culture manager $this->initialize(); }