private function compile() { $rawDirsPattern = '/^(' . implode('|', $this->rawDirs) . ')(\\/|$)/i'; foreach (Finder::create()->files()->in($this->assetsPath) as $file) { $relativePath = $file->getRelativePath(); if (empty($relativePath) || preg_match($rawDirsPattern, $relativePath)) { continue; } if (substr($file->getBasename(), 0, 1) === '_') { continue; } $asset = Asset::make($file->getRealPath()); $process = $asset->getCompileProcess(); $this->info($process->getCommandline()); $process->setEnv(['PATH' => trim(`echo \$PATH`) . ':/usr/local/bin/']); $out = ''; $err = ''; $status = $process->run(function ($type, $line) use(&$out, &$err) { if ($type === 'out') { $out .= $line . "\n"; $err .= $line . "\n"; } else { if ($type === 'err') { $err .= $line . "\n"; } } }); $name = $file->getRelativePathname(); $name = $asset->getType() . substr($name, strpos($name, '/')); $name = substr($name, 0, strrpos($name, '.')) . '-' . md5($out) . '.' . $asset->getType(); if ($status === 0) { $this->storeAsset($file->getRealPath(), $this->publishPath . '/' . $name, $out, $asset->getLastModified()); } else { $this->error($err); exit(1); } unset($out, $err, $process); } }
private function compile() { foreach ($this->findAssets($this->assetsPath, $this->rawDirs) as $file) { $relativePath = $file->getRelativePath(); if (empty($relativePath)) { continue; } if (substr($file->getBasename(), 0, 1) === '_') { continue; } $path = $file->getRealPath(); $asset = Asset::make($path); $out = null; try { $this->info('Compiling ' . $path); $out = $asset->compile(); } catch (CompilationException $e) { $this->error($e->getMessage()); if (is_array($e->context)) { foreach ($e->context as $key => $value) { $this->error(' --> ' . strtoupper($key) . ': ' . $value); } } else { if (is_string($e->context)) { $this->error(' --> ' . $e->context); } } if (!empty($e->log)) { $this->error(' --> ' . $e->log); } exit(1); } $name = $file->getRelativePathname(); $name = $asset->getType() . substr($name, strpos($name, '/')); $name = substr($name, 0, strrpos($name, '.')) . '-' . md5($out) . '.' . $asset->getType(); $this->storeAsset($file->getRealPath(), $this->publishPath . '/' . $name, $out, $asset->getLastModified()); unset($out, $err, $asset); } }
/** * Gets the path for the request assets and handles caching/etag responses * Automatically sends a 404 and exits if path doesn't exist or fails a security check * * @param string $contentType * @param Asset $asset Asset to compile. If null, path contents will be ouputed * @param int $lastModified If null, filemtime will be used, should return a unix timestamp */ private function process($contentType, Asset $asset = null, $lastModified = null) { if ($lastModified === null) { $lastModified = filemtime($this->path); } $etag = '"' . sha1($this->path . $lastModified) . '"'; if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) { if ($_SERVER['HTTP_IF_NONE_MATCH'] === $etag) { header('HTTP/1.1 304 Not Modified'); exit; } } $dateFormat = 'D, d M Y H:i:s T'; header('Cache-Control: public, max-age=31536000'); header('Expires: ' . gmdate($dateFormat, $lastModified + 31536000)); header('Last-Modified: ' . gmdate($dateFormat, $lastModified)); header('ETag: ' . $etag); header('Content-Type: ' . $contentType . '; charset=utf-8'); $compile = function () use($asset) { if ($asset === null) { return file_get_contents($this->path); } else { header('X-Cached: false'); try { return $asset->compile(); } catch (CompilationException $e) { if (is_array($e->context)) { foreach ($e->context as $key => $value) { printf('/* %s: %s */%s', strtoupper($key), $value, PHP_EOL); } } else { if (is_string($e->context)) { printf('/* %s */%s', $e->context, PHP_EOL); } } echo $e->log; exit(1); } } }; if (isset($_GET['ignore-cache'])) { echo $compile(); } else { echo app('cache')->remember(str_slug($this->path) . '-' . md5($lastModified), 1440, $compile); } exit; }
/** * Get a path to your asset * * @param mixed $path Asset path relative to resources/assets * @return string */ function asset_path($path) { return \Assets\Asset::publishedPath($path); }
public function compile($type) { $asset = Asset::make($this->path); return $this->process($asset->getMime(), $asset->getCompileProcess(), $asset->getLastModified()); }