/** * Save a cached file of the output * * @param $content */ protected function writeCacheFile($content) { $dir = $this->temp_dir . DIRECTORY_SEPARATOR . 'synergy'; Logger::debug('Synergy cache dir: ' . $dir); if (!is_dir($dir)) { Tools::mkdir($dir); } $file = $dir . DIRECTORY_SEPARATOR . md5($this->request->getUri()) . '.syn'; $fh = fopen($file, 'w'); fputs($fh, $content, strlen($content)); @fclose($fh); if (!$this->isDev && $this->useGzip()) { Logger::info('Compressing response'); $zp = gzopen($file . '.gz', 'w9'); gzwrite($zp, $content); gzclose($zp); // remove gzip file if it's bigger than the unzipped file if (filesize($file . '.gz') > filesize($file)) { unlink($file . '.gz'); } } }
/** * Tries to match a URL with a set of routes. * Will always return the first route that matches. * * @param string $pathinfo The path info to be parsed * @param RouteCollection $routes The set of routes * * @return array An array of parameters * * @throws ResourceNotFoundException If the resource could not be found * @throws MethodNotAllowedException method is not allowed */ protected function matchCollection($pathinfo, RouteCollection $routes) { /** * @var $route \Symfony\Component\Routing\Route */ foreach ($routes as $name => $route) { $compiledRoute = $route->compile(); // check the static prefix of the URL first. // Only use the more expensive preg_match when it matches if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($pathinfo, $compiledRoute->getStaticPrefix())) { continue; } if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) { continue; } $hostMatches = array(); if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) { continue; } // check HTTP method requirement if ($req = $route->getRequirement('_method')) { // HEAD and GET are equivalent as per RFC if ('HEAD' === ($method = $this->context->getMethod())) { $method = 'GET'; } if (!in_array($method, $req = explode('|', strtoupper($req)))) { $this->allow = array_merge($this->allow, $req); continue; } } // check device if (class_exists('\\Mobile_Detect') && $this->context->getDevice() instanceof \Mobile_Detect) { /** * @var $device \Mobile_Detect */ $device = $this->context->getDevice(); $deviceType = $device->isMobile() ? $device->isTablet() ? 'tablet' : 'mobile' : 'computer'; if ($route->getOption('device') && $route->getOption('device') != $deviceType) { continue; } /** * Test for a specific mobile OS */ if ($routeOS = $route->getOption('os')) { $deviceOS = $this->matchDeviceOS($routeOS, $device); if ($deviceOS) { Logger::info("Matched RouteOS: " . $deviceOS); } else { continue; } } /** * Test for a specific mobile/tablet type (brand) * eg iPhone, BlackBerry, HTC, Dell, etc */ if ($device->isMobile() && !$device->isTablet() && ($routeType = $route->getOption('type'))) { $phoneType = $this->matchPhoneType($routeType, $device); if ($phoneType) { Logger::info("Matched RouteType: " . $phoneType); } else { continue; } } elseif ($device->isTablet() && ($routeType = $route->getOption('type'))) { $tabletType = $this->matchTabletType($routeType, $device); if ($tabletType) { Logger::info("Matched RouteType: " . $tabletType); } else { continue; } } } $status = $this->handleRouteRequirements($pathinfo, $name, $route); if (self::ROUTE_MATCH === $status[0]) { return $status[1]; } if (self::REQUIREMENT_MISMATCH === $status[0]) { continue; } return $this->getAttributes($route, $name, array_replace($matches, $hostMatches)); } return false; }
/** * Look for a template file within the matchDir * * @param string $matchDir * @param string $file * * @return SmartyTemplate|TwigTemplate|HtmlTemplate|null */ protected function matchTemplate($matchDir, $file) { if (file_exists($matchDir . $file . '.tpl') && class_exists('\\Smarty')) { Logger::info('Smarty Template: ' . $file . '.tpl'); $template = new SmartyTemplate(); $template->setTemplateDir($matchDir); $template->setTemplateFile($file . '.tpl'); return $template; } elseif (file_exists($matchDir . $file . '.twig') && class_exists('\\Twig_Environment')) { Logger::info('Twig Template: ' . $file . '.twig'); $template = new TwigTemplate(); $template->setTemplateDir($matchDir); $template->setTemplateFile($file . '.twig'); return $template; } elseif (file_exists($matchDir . $file)) { Logger::info('HTML: ' . $file); $template = new HtmlTemplate(); $template->setTemplateDir($matchDir); $template->setTemplateFile($file); return $template; } }