/** * @param array $attributes * @return object */ public function createObject(array $attributes) { $className = 'Serphlet\\Config\\ServletMap'; // Instantiate the new object and return it $config = null; try { $config = \Serphlet\ClassLoader::newInstance($className, 'Serphlet\\Config\\ServletMap'); } catch (\Exception $e) { $this->digester->getLogger()->error('Serphlet\\Config\\ServletMapFactory->createObject(): ' . $e->getMessage()); } return $config; }
/** * Maps the current PHP request to the correct servlet configuration */ protected function map() { // TODO: Write a mapper class to fully implement matching // Look through servlet mappings and do a match $pathInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null; // Check for configured servlets $servlets = $this->context->getServlets(); if (empty($servlets)) { throw new \Serphlet\Exception\UnavailableException('No servlets configured'); } // Attempt to match urls $urlMapped = array(); foreach ($servlets as $servlet) { $urlMappings = $servlet->getMappings(); if (!empty($urlMappings)) { foreach ($urlMappings as $urlMapping) { // Is this a file extension method (but not a path method) if (!empty($pathInfo) && substr($urlMapping, 0, 2) == '*.') { $suffix = substr($urlMapping, 1); if (strpos($pathInfo, '.') > 0 && substr($pathInfo, -1 * strlen($suffix)) == $suffix) { $urlMapped[$urlMapping] = $servlet; } } // /servlet/path/* method (or default /*), but not with an extension present if (substr($urlMapping, -2) == '/*' && (strpos($pathInfo, '.') === false || strlen($urlMapping) == 2)) { $prefix = substr($urlMapping, 0, -1); $ignoreTrailingSlashPathInfo = substr($pathInfo, -1) != '/' ? $pathInfo . '/' : $pathInfo; if (strpos($ignoreTrailingSlashPathInfo, $prefix) === 0) { // Prefix matches the path info $urlMapped[$urlMapping] = $servlet; } } } } } // TODO: Consider the welcome files (when empty path info) // Match the 'best' (based on the most specific match) $pattern = ''; if (count($urlMapped) == 1) { // Only one matched $pattern = key($urlMapped); $this->config = current($urlMapped); } else { // Match the most specific one $matches = array_keys($urlMapped); foreach ($matches as $match) { if (strlen($match) > strlen($pattern)) { $pattern = $match; } } if (!empty($pattern)) { $this->config = $urlMapped[$pattern]; } } // Check the result if (empty($this->config)) { throw new \Serphlet\Exception\UnavailableException('No servlets configured'); } $this->config->setServletMapping($pattern); // Setup the request paths $requestPathInfo = ''; $requestServletPath = ''; if (strpos($pattern, '.') > -1) { // Extension match, ditch the extension in the path info $requestPathInfo = substr($pathInfo, 0, -1 * strlen(substr($pattern, 1))); } else { // Path pattern $requestServletPath = substr($pattern, 0, -2); $requestPathInfo = substr($pathInfo, strlen($requestServletPath)); } if (substr($requestPathInfo, 0, 1) !== '/') { $requestPathInfo = '/' . $requestPathInfo; } $this->request->setPathInfo($requestPathInfo); $this->request->setServletPath($requestServletPath); // Factory the matching servlet classname for the request try { $className = $this->config->getServletClass(); $servlet = \Serphlet\ClassLoader::newInstance($className, '\\Serphlet\\ServletInterface'); } catch (Exception $e) { // TODO: Handle throw $e; } return $servlet; }