+++ /dev/null
-<?php
-
-class IPF_Assets
-{
- private $files = array();
- private $dirs = array();
-
- public function __construct($dir)
- {
- $this->dirs = array(
- rtrim($dir, '/'),
- IPF::get('project_root') . '/vendor/npm-asset',
- IPF::get('project_root') . '/vendor/bower-asset',
- );
- }
-
- public function compileFile($path)
- {
- $root = $this->path($path);
- list($success, $data) = \PFF\TopSort::sort(array($root), array($this, 'following'));
- if (!$success)
- throw new Exception('Loop detected: '.implode(' -> ', $data));
-
- $dump = array();
- foreach ($data as $path) {
- $dump[] = $this->files[$path];
- }
- return implode("\n", $dump);
- }
-
- public static function compile($dir, $path)
- {
- $compiler = new IPF_Assets($dir);
- return $compiler->compileFile($path);
- }
-
- function following($path)
- {
- if (!array_key_exists($path, $this->files))
- $this->files[$path] = file_get_contents($path);
- $content = $this->files[$path];
-
- if (!preg_match_all('#^(//=\\s*require\\s*(\\S+)|/\\*=\\s*require\\s*(\\S+)\\s*\\*/)\\s*$#m', $content, $matches, PREG_SET_ORDER))
- return array();
-
- $currentDir = dirname($path);
-
- $result = array();
- foreach ($matches as $m) {
- $result[] = $this->path(@$m[2].@$m[3], $currentDir);
- }
-
- return $result;
- }
-
- private function possiblePaths($file, $currentDir=null)
- {
- $file = ltrim($file, '/');
- $paths = array();
-
- if ($currentDir)
- $paths[] = rtrim($currentDir, '/') . '/' . $file;
-
- foreach ($this->dirs as $base)
- $paths[] = $base . '/' . $file;
-
- return $paths;
- }
-
- function path($file, $currentDir=null)
- {
- $paths = array_filter(array_map('realpath', $this->possiblePaths($file, $currentDir)));
- if (!$paths)
- throw new Exception("File '$file' not found in '" . implode("', '", $this->dirs) . "'.");
- return array_shift($paths);
- }
-}
-
foreach ($this->apps as $app) {
$source = $app->getPath() . 'static';
- if (is_dir($source))
+ if (is_dir($source)) {
IPF_Utils::copyDirectory($source, $this->staticDir);
-
- foreach ($app->assets() as $asset => $output) {
- $compiler = new IPF_Assets($app->getPath());
-
- $src = $compiler->path($asset);
- $dest = $this->staticDir . $output;
-
- if (is_dir($src)) {
- IPF_Utils::copyDirectory($src, $dest);
- } else {
- $content = $compiler->compileFile($asset);
- IPF_Utils::makeDirectories(dirname($dest));
- file_put_contents($dest, $content);
- }
}
}
}
$static = $app->getPath() . $staticUrl . $query;
if (is_file($static))
return new IPF_HTTP_Response_File($static, null, $this->mimetype($query));
-
- $asset = array_search($query, $app->assets());
- if ($asset !== false) {
- $content = IPF_Assets::compile($app->getPath(), $asset);
- return new IPF_HTTP_Response($content, $this->mimetype($query));
- }
}
return false;