/** * handle the view layer * * @param string $class current requested controller class * @param string $method current requested controller method or action * @param array $data array containing all data to be assigned to view * @param \Alchemy\Annotation\Annotation $annotation object containing information about view object * @throws \Exception * @return \Alchemy\Mvc\ViewInterface $view The create view object */ protected function handleView($class, $method, $data, $annotation) { // check if a @view definition exists on method's annotations if (empty($annotation)) { return null; // no @view annotation found, just return to break view handling } $annotation->prepare(); $template = $annotation->template; // check if template filename is empty if (empty($template)) { // that means the annotation "@view" was not set // Then we compose a template filename using controller class and method names but // removing ...Controller & ..Action suffixes from those names $nsSepPos = strrpos($class, '\\'); $class = preg_replace('/([A-Z])/', '_$1', substr($class, $nsSepPos + 1, -10)); $class = strtolower(ltrim($class, "_")); $method = substr($method, 0, -6); $template = $class . DS . $method; } $view = $this->createView($template, $data, $annotation->engine); $registeredVars = array(); // for javascript annotated if ($registerAnnotation = $this->annotationReader->getAnnotation('RegisterJavascript')) { $registeredVars = $registerAnnotation->all(); $publicBasePath = $this->config->get('app.public_dir') . DS; $sourceBasePath = $this->config->get('app.view_scripts_javascript_dir') . DS; $jsBaseUrl = 'assets/cache/'; foreach ($registeredVars as $i => $jsFile) { if (!file_exists($sourceBasePath . $jsFile)) { throw new \Exception(sprintf("File Not Found Error: File %s doesn't exist!.", $jsFile)); } $jsPath = substr($jsFile, 0, strrpos($jsFile, DS)); $fileSum = md5_file($sourceBasePath . $jsFile); $jsCacheFile = rtrim($jsFile, '.js') . '-' . $fileSum . '.js'; // verify if the source file was modified since last cache generated version if (!is_file($publicBasePath . $jsBaseUrl . $jsCacheFile)) { if (!is_dir($publicBasePath . $jsBaseUrl . $jsPath)) { self::createDir($publicBasePath . $jsBaseUrl . $jsPath); } // clean up -! Remove old cache file $oldCacheFiles = glob($publicBasePath . $jsBaseUrl . rtrim($jsFile, '.js') . '-*'); if (count($oldCacheFiles) > 0) { foreach ($oldCacheFiles as $oldCacheFile) { @unlink($oldCacheFile); } } // TODO copy a minified version instead a normal copy, it depends if it is a debug env. copy($sourceBasePath . $jsFile, $publicBasePath . $jsBaseUrl . $jsCacheFile); } // adding the new js file path to assign as variable to view object $registeredVars[$i] = $jsBaseUrl . $jsCacheFile; } } // end javascript registration // registering variables $view->assign('javascript', $registeredVars); return $view; }