/** * Recursively process asset * @param array $dependencies Collection of assets for compilation */ protected function processAsset($dependencies) { foreach ($dependencies as $source => $nothing) { // Read asset content $content = $this->fileManager->read($source); $extension = pathinfo($source, PATHINFO_EXTENSION); // Resource dependant resources $innerDependencies = []; // Compile content $compiled = $content; Event::fire(self::E_COMPILE, [$source, &$extension, &$compiled, &$innerDependencies]); // Write compiled asset $target = $this->getAssetProcessedPath($source); $this->fileManager->write($target, $compiled); $this->fileManager->touch($target, $this->fileManager->lastModified($source)); // Go deeper in recursion $this->processAsset($innerDependencies); } }
/** * Recursively replace import in content of the LESS file * * @param string $resource Resource full path * @param string $content less file content * * @return string Content of LESS file with included imported resources * @throws ResourceNotFound If importing resource could not be found */ protected function readImport($resource, $content) { // Rewrite imports $matches = []; if (preg_match_all(self::P_IMPORT_DECLARATION, $content, $matches)) { for ($i = 0, $size = count($matches[0]); $i < $size; $i++) { // Build absolute path to imported resource $path = dirname($resource) . DIRECTORY_SEPARATOR . $matches['path'][$i]; // Append .less extension according to standard if (false === ($path = realpath($this->fileManager->exists($path) ? $path : $path . '.less'))) { throw new ResourceNotFound('Cannot import file: ' . $matches['path'][$i]); } // Add parent to child dependency $this->dependencies[$path][$resource] = []; // Replace path in LESS @import command with recursive call to this function $content = str_replace($matches[0][$i], $this->readImport($path, $this->fileManager->read($path)), $content); } } return $content; }