Example #1
0
 function create_full_cache($pages = null)
 {
     $search_fields = array('url', 'file_path', 'title', 'author', 'content');
     $store = array();
     if (!isset($pages)) {
         $pages = Helpers::file_cache('./content');
     }
     foreach ($pages as $page) {
         if ($page['is_folder']) {
             $current_page = AssetFactory::get($page['path']);
             # Skip for password protected pages
             if ($current_page['password_protect'] || $current_page['hide_from_search']) {
                 continue;
             }
             # Only save search field data
             foreach ($current_page as $key => $value) {
                 if (!in_array($key, $search_fields)) {
                     unset($current_page[$key]);
                 }
             }
             $store[] = $current_page;
             $children = self::create_full_cache(Helpers::file_cache($page['path']));
             if (is_array($children)) {
                 $store = array_merge($store, $children);
             }
         }
     }
     return $store;
 }
Example #2
0
 /**
  * @todo support for import-once
  * @todo support for import (less) "lib.css"
  */
 public function getChildren(AssetFactory $factory, $content, $loadPath = null)
 {
     $loadPaths = array();
     //$this->loadPaths;
     if (null !== $loadPath) {
         $loadPaths[] = $loadPath;
     }
     if (empty($loadPaths)) {
         return array();
     }
     $children = array();
     foreach (LessUtils::extractImports($content) as $reference) {
         if ('.css' === substr($reference, -4)) {
             // skip normal css imports
             // todo: skip imports with media queries
             continue;
         }
         if ('.less' !== substr($reference, -5)) {
             $reference .= '.less';
         }
         foreach ($loadPaths as $loadPath) {
             if (file_exists($file = $loadPath . '/' . $reference)) {
                 $coll = $factory->createAsset($file, array(), array('root' => $loadPath));
                 foreach ($coll as $leaf) {
                     $leaf->ensureFilter($this);
                     $children[] = $leaf;
                     goto next_reference;
                 }
             }
         }
         next_reference:
     }
     return $children;
 }
Example #3
0
 public function resolve($asset, Asset $relative = null)
 {
     if ($relative) {
         return AssetFactory::fromAsset($relative, $asset);
     }
     return AssetFactory::fromUrl($asset, $this->_env);
 }
 function create_full_cache($pages = null)
 {
     $search_fields = array('url', 'file_path', 'title', 'author', 'content', 'object_name', 'designer', 'builder', 'category', 'tags', 'contributors', 'realisation_place', 'required_hardware', 'license', 'client');
     $store = array();
     if (!isset($pages)) {
         $pages = Helpers::file_cache('./content');
     }
     foreach ($pages as $page) {
         if ($page['is_folder']) {
             $current_page = AssetFactory::get($page['path']);
             # Skip for password protected pages
             if (isset($current_page['password_protect']) || isset($current_page['hide_from_search'])) {
                 continue;
             }
             # Only save search field data
             foreach ($current_page as $key => $value) {
                 if (!in_array($key, $search_fields)) {
                     unset($current_page[$key]);
                 }
             }
             $store[] = $current_page;
             $children = self::create_full_cache(Helpers::file_cache($page['path']));
             if (is_array($children)) {
                 $store = array_merge($store, $children);
             }
         }
     }
     return $store;
 }
Example #5
0
 public function assetTimestamp($path)
 {
     try {
         return '/asset' . AssetFactory::fromUrl($path, $this->env)->digestUrl();
     } catch (Exception $e) {
         return parent::assetTimestamp($path);
     }
 }
 public function dispatch($url, CakeResponse $response)
 {
     try {
         $asset = AssetFactory::fromUrl($url, $this->_env);
         $this->_deliver($response, $asset);
         if (Configure::read('debug') == 0) {
             $File = new File(WWW_ROOT . 'asset' . DS . str_replace('/', DS, $asset->digestUrl()), true);
             $File->write($asset->content());
         }
     } catch (Exception $e) {
         $response->statusCode(404);
         $response->send();
     }
     return;
 }
 /**
  * Combines assets matching a pattern to a single file asset, optionally applies filters.
  *
  * @param  AssetCollection $assets
  * @param  string          $name
  * @param  array           $options
  * @return AssetCollection
  */
 protected function doCombine(AssetCollection $assets, $name, $options = [])
 {
     extract($options);
     $combine = new AssetCollection();
     $pattern = $this->globToRegex($pattern);
     foreach ($assets as $asset) {
         if (preg_match($pattern, $asset->getName())) {
             $combine->add($asset);
         }
     }
     $file = strtr($this->cache, ['%name%' => $name]);
     if ($names = $combine->names() and $file = $this->doCache($combine, $file, $filters)) {
         $assets->remove(array_slice($names, 1));
         $assets->replace(array_shift($names), $this->factory->create($name, $file));
     }
     return $assets;
 }
 public function testFromAsset()
 {
     $Asset = new CssAsset('css/default.css', $this->file, $this->path);
     $result = AssetFactory::fromAsset($Asset, 'bundle');
     $this->assertInstanceOf('Asset', $result);
     $this->assertEquals('css/bundle.css', $result->url);
     $this->assertEquals($this->path . 'css' . DS . 'bundle.css', $result->file);
     $Asset = new CssAsset('css/app/bundle.css', $this->file, $this->path);
     $result = AssetFactory::fromAsset($Asset, '../bundle');
     $this->assertInstanceOf('Asset', $result);
     $this->assertEquals('css/bundle.css', $result->url);
     $this->assertEquals($this->path . 'css' . DS . 'bundle.css', $result->file);
     $Asset = new CssAsset('css/default.css', $this->file, $this->path);
     $result = AssetFactory::fromAsset($Asset, 'app/bundle');
     $this->assertInstanceOf('Asset', $result);
     $this->assertEquals('css/app/bundle.css', $result->url);
     $this->assertEquals($this->path . 'css' . DS . 'app' . DS . 'bundle.css', $result->file);
     $result = AssetFactory::fromAsset($Asset, '/theme/admin/css/bundle');
     $this->assertInstanceOf('Asset', $result);
     $this->assertEquals('theme/admin/css/bundle.css', $result->url);
     $this->assertEquals(App::themePath('admin') . 'webroot' . DS . 'css' . DS . 'bundle.css', $result->file);
 }
 static function parse_foreach($data, $template)
 {
     # split out the partial into the parts Before, Inside, and After the foreach loop
     preg_match('/([\\S\\s]*?)foreach[\\s]+?([\\$\\@].+?)\\s+?do\\s+?([\\S\\s]+?)endforeach([\\S\\s]*)$/', $template, $template_parts);
     # run the replacements on the pre-"foreach" part of the partial
     $template = self::parse($data, $template_parts[1]);
     # traverse one level deeper into the data hierachy
     $pages = isset($data[$template_parts[2]]) && is_array($data[$template_parts[2]]) && !empty($data[$template_parts[2]]) ? $data[$template_parts[2]] : false;
     # check for any nested matches
     $template_parts = self::test_nested_matches($template_parts, 'foreach[\\s]+?[\\$\\@].+?\\s+?do\\s+?', 'endforeach');
     if ($pages) {
         foreach ($pages as $data_item) {
             # transform data_item into its appropriate Object
             $data_object =& AssetFactory::get($data_item);
             # recursively parse the inside part of the foreach loop
             $template .= self::parse($data_object, $template_parts[3]);
         }
     }
     # run the replacements on the post-"foreach" part of the partial
     $template .= self::parse($data, $template_parts[4]);
     return $template;
 }
 function sortby($object, $value)
 {
     $this->sortby_value = $value;
     $sorted = array();
     # expand sub variables if required
     if (is_array($object)) {
         foreach ($object as $key) {
             if (is_string($key)) {
                 $sorted[] =& AssetFactory::get($key);
             }
         }
     }
     # sort the array
     uasort($sorted, array($this, 'custom_str_sort'));
     return $sorted;
 }
 static function parse_foreach($data, $template)
 {
     # split out the partial into the parts Before, Inside, and After the foreach loop
     preg_match('/([\\S\\s]*?)foreach[\\s]+?([\\$\\@].+?)\\s+?do\\s+?([\\S\\s]+?)endforeach([\\S\\s]*)$/', $template, $template_parts);
     # run the replacements on the pre-"foreach" part of the partial
     $template = self::parse($data, $template_parts[1]);
     # allow loop limiting
     if (preg_match('/\\[\\d*:\\d*\\]$/', $template_parts[2])) {
         preg_match('/([\\$\\@].+?)\\[(\\d*):(\\d*)\\]$/', $template_parts[2], $matches);
         $template_parts[2] = $matches[1];
         $start_limit = empty($matches[2]) ? 0 : $matches[2];
         if (!empty($matches[3])) {
             $end_limit = $matches[3];
         }
     }
     # traverse one level deeper into the data hierachy
     $pages = isset($data[$template_parts[2]]) && is_array($data[$template_parts[2]]) && !empty($data[$template_parts[2]]) ? $data[$template_parts[2]] : false;
     # slice down the data array if required
     if (is_array($pages) && isset($start_limit)) {
         $pages = array_slice($pages, $start_limit, $end_limit);
     }
     # check for any nested matches
     $template_parts = self::test_nested_matches($template_parts, 'foreach[\\s]+?[\\$\\@].+?\\s+?do\\s+?', 'endforeach');
     if ($pages) {
         foreach ($pages as $data_item) {
             # transform data_item into its appropriate Object
             $data_object =& AssetFactory::get($data_item);
             # recursively parse the inside part of the foreach loop
             $template .= self::parse($data_object, $template_parts[3]);
         }
     }
     # run the replacements on the post-"foreach" part of the partial
     $template .= self::parse($data, $template_parts[4]);
     return $template;
 }
Example #12
0
 static function parse_foreach($data, $template)
 {
     # split out the partial into the parts Before, Inside, and After the foreach loop
     preg_match('/([\\S\\s]*?)foreach[\\s]+?([\\$\\@].+?)\\s+?do\\s+?([\\S\\s]+?)endforeach([\\S\\s]*)$/', $template, $template_parts);
     # run the replacements on the pre-"foreach" part of the partial
     $template = self::parse($data, $template_parts[1]);
     # new: allow limitation syntax
     # e.g. $children[:5] to only get first 5 children
     preg_match('/([\\$\\@a-z0-9_].+?)([\\[\\]\\:\\d]+?|)$/', $template_parts[2], $limit);
     # if there is a limit set in the template
     if ($limit[2]) {
         preg_match('/\\[\\:([\\d]+?)\\]/', $limit[2], $limit[2]);
         # so here is the limit of things to get:
         $slice = $limit[2][1];
     } else {
         $slice = false;
     }
     # i'm lazy. i won't break anything i have to fix afterwards.
     $template_parts[2] = $limit[1];
     # traverse one level deeper into the data hierachy
     $pages = isset($data[$template_parts[2]]) && is_array($data[$template_parts[2]]) && !empty($data[$template_parts[2]]) ? $data[$template_parts[2]] : false;
     # check for any nested matches
     $template_parts = self::test_nested_matches($template_parts, 'foreach[\\s]+?[\\$\\@].+?\\s+?do\\s+?', 'endforeach');
     if ($pages) {
         # slice the array according to set limit
         if ($slice) {
             $pages = array_slice($pages, 0, $slice);
         }
         foreach ($pages as $data_item) {
             # transform data_item into its appropriate Object
             $data_object =& AssetFactory::get($data_item);
             # recursively parse the inside part of the foreach loop
             $template .= self::parse($data_object, $template_parts[3]);
         }
     }
     # run the replacements on the post-"foreach" part of the partial
     $template .= self::parse($data, $template_parts[4]);
     return $template;
 }
 /**
  * Returns the public path of an asset
  *
  * @return string A public path
  */
 public function asseticBlock(array $params = array(), $content = null, $template, &$repeat)
 {
     // In debug mode, we have to be able to loop a certain number of times, so we use a static counter
     static $count;
     static $assetsUrls;
     // Read config file
     if (isset($params['config_path'])) {
         $base_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $params['config_path'];
     } else {
         // Find the config file in Symfony2 config dir
         $base_path = __DIR__ . '/../../../../app/config/smarty-assetic';
     }
     $config = json_decode(file_get_contents($base_path . '/config.json'));
     // Opening tag (first call only)
     if ($repeat) {
         // Read bundles and dependencies config files
         $bundles = json_decode(file_get_contents($base_path . '/bundles.json'));
         $dependencies = json_decode(file_get_contents($base_path . '/dependencies.json'));
         $am = new AssetManager();
         $fm = new FilterManager();
         $fm->set('yui_js', new Filter\Yui\JsCompressorFilter($config->yuicompressor_path, $config->java_path));
         $fm->set('yui_css', new Filter\Yui\CssCompressorFilter($config->yuicompressor_path, $config->java_path));
         $fm->set('less', new Filter\LessphpFilter());
         $fm->set('sass', new Filter\Sass\SassFilter());
         $fm->set('closure_api', new Filter\GoogleClosure\CompilerApiFilter());
         $fm->set('closure_jar', new Filter\GoogleClosure\CompilerJarFilter($config->closurejar_path, $config->java_path));
         // Factory setup
         $factory = new AssetFactory($_SERVER['DOCUMENT_ROOT']);
         $factory->setAssetManager($am);
         $factory->setFilterManager($fm);
         $factory->setDefaultOutput('assetic/*.' . $params['output']);
         if (isset($params['filter'])) {
             $filters = explode(',', $params['filter']);
         } else {
             $filters = array();
         }
         // Prepare the assets writer
         $writer = new AssetWriter($params['build_path']);
         // If a bundle name is provided
         if (isset($params['bundle'])) {
             $asset = $factory->createAsset($bundles->{$params}['output']->{$params}['bundle'], $filters, array($params['debug']));
             $cache = new AssetCache($asset, new FilesystemCache($params['build_path']));
             $writer->writeAsset($cache);
             // If individual assets are provided
         } elseif (isset($params['assets'])) {
             $assets = array();
             // Include only the references first
             foreach (explode(',', $params['assets']) as $a) {
                 // If the asset is found in the dependencies file, let's create it
                 // If it is not found in the assets but is needed by another asset and found in the references, don't worry, it will be automatically created
                 if (isset($dependencies->{$params}['output']->assets->{$a})) {
                     // Create the reference assets if they don't exist
                     foreach ($dependencies->{$params}['output']->assets->{$a} as $ref) {
                         try {
                             $am->get($ref);
                         } catch (InvalidArgumentException $e) {
                             $assetTmp = $factory->createAsset($dependencies->{$params}['output']->references->{$ref});
                             $am->set($ref, $assetTmp);
                             $assets[] = '@' . $ref;
                         }
                     }
                 }
             }
             // Now, include assets
             foreach (explode(',', $params['assets']) as $a) {
                 // Add the asset to the list if not already present, as a reference or as a simple asset
                 $ref = null;
                 if (isset($dependencies->{$params}['output'])) {
                     foreach ($dependencies->{$params}['output']->references as $name => $file) {
                         if ($file == $a) {
                             $ref = $name;
                             break;
                         }
                     }
                 }
                 if (array_search($a, $assets) === FALSE && ($ref === null || array_search('@' . $ref, $assets) === FALSE)) {
                     $assets[] = $a;
                 }
             }
             // Create the asset
             $asset = $factory->createAsset($assets, $filters, array($params['debug']));
             $cache = new AssetCache($asset, new FilesystemCache($params['build_path']));
             $writer->writeAsset($cache);
         }
         // If debug mode is active, we want to include assets separately
         if ($params['debug']) {
             $assetsUrls = array();
             foreach ($asset as $a) {
                 $cache = new AssetCache($a, new FilesystemCache($params['build_path']));
                 $writer->writeAsset($cache);
                 $assetsUrls[] = $a->getTargetPath();
             }
             // It's easier to fetch the array backwards, so we reverse it to insert assets in the right order
             $assetsUrls = array_reverse($assetsUrls);
             $count = count($assetsUrls);
             if (isset($config->site_url)) {
                 $template->assign($params['asset_url'], $config->site_url . '/' . $params['build_path'] . '/' . $assetsUrls[$count - 1]);
             } else {
                 $template->assign($params['asset_url'], '/' . $params['build_path'] . '/' . $assetsUrls[$count - 1]);
             }
             // Production mode, include an all-in-one asset
         } else {
             if (isset($config->site_url)) {
                 $template->assign($params['asset_url'], $config->site_url . '/' . $params['build_path'] . '/' . $asset->getTargetPath());
             } else {
                 $template->assign($params['asset_url'], '/' . $params['build_path'] . '/' . $asset->getTargetPath());
             }
         }
         // Closing tag
     } else {
         if (isset($content)) {
             // If debug mode is active, we want to include assets separately
             if ($params['debug']) {
                 $count--;
                 if ($count > 0) {
                     if (isset($config->site_url)) {
                         $template->assign($params['asset_url'], $config->site_url . '/' . $params['build_path'] . '/' . $assetsUrls[$count - 1]);
                     } else {
                         $template->assign($params['asset_url'], '/' . $params['build_path'] . '/' . $assetsUrls[$count - 1]);
                     }
                 }
                 $repeat = $count > 0;
             }
             return $content;
         }
     }
 }
 /**
  * Returns the attribute value for a given array/object.
  *
  * @param mixed   $object            The object or array from where to get the item
  * @param mixed   $item              The item to get from the array or object
  * @param array   $arguments         An array of arguments to pass if the item is an object method
  * @param string  $type              The type of attribute (@see Twig_TemplateInterface)
  * @param Boolean $isDefinedTest     Whether this is only a defined check
  * @param Boolean $ignoreStrictCheck Whether to ignore the strict attribute check or not
  *
  * @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
  *
  * @throws Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
  */
 protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
 {
     $item = (string) $item;
     // Stacey-specific
     // If the attribute is a string path, then we need to pass it through the asset factory to create its page variables
     if (is_string($object)) {
         $object =& AssetFactory::get($object);
     }
     // array
     if (Twig_TemplateInterface::METHOD_CALL !== $type) {
         if (is_array($object) && array_key_exists($item, $object) || $object instanceof ArrayAccess && isset($object[$item])) {
             if ($isDefinedTest) {
                 return true;
             }
             return $object[$item];
         }
         if (Twig_TemplateInterface::ARRAY_CALL === $type) {
             if ($isDefinedTest) {
                 return false;
             }
             if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
                 return null;
             }
             if (is_object($object)) {
                 throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
                 // array
             } else {
                 throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
             }
         }
     }
     if (!is_object($object)) {
         if ($isDefinedTest) {
             return false;
         }
         if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
             return null;
         }
         throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object));
     }
     $class = get_class($object);
     // object property
     if (Twig_TemplateInterface::METHOD_CALL !== $type) {
         /* apparently, this is not needed as this is already covered by the array_key_exists() call below
            if (!isset(self::$cache[$class]['properties'])) {
                foreach (get_object_vars($object) as $k => $v) {
                    self::$cache[$class]['properties'][$k] = true;
                }
            }
            */
         if (isset($object->{$item}) || array_key_exists($item, $object)) {
             if ($isDefinedTest) {
                 return true;
             }
             if ($this->env->hasExtension('sandbox')) {
                 $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
             }
             return $object->{$item};
         }
     }
     // object method
     if (!isset(self::$cache[$class]['methods'])) {
         self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
     }
     $lcItem = strtolower($item);
     if (isset(self::$cache[$class]['methods'][$lcItem])) {
         $method = $item;
     } elseif (isset(self::$cache[$class]['methods']['get' . $lcItem])) {
         $method = 'get' . $item;
     } elseif (isset(self::$cache[$class]['methods']['is' . $lcItem])) {
         $method = 'is' . $item;
     } elseif (isset(self::$cache[$class]['methods']['__call'])) {
         $method = $item;
     } else {
         if ($isDefinedTest) {
             return false;
         }
         if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
             return null;
         }
         throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
     }
     if ($isDefinedTest) {
         return true;
     }
     if ($this->env->hasExtension('sandbox')) {
         $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
     }
     $ret = call_user_func_array(array($object, $method), $arguments);
     if ($object instanceof Twig_TemplateInterface) {
         return new Twig_Markup($ret);
     }
     return $ret;
 }
Example #15
0
 /**
  * Resolve name of assets to include
  * in this context and populate the list of assets.
  */
 private function resolveInclusions()
 {
     $includes = $this->getInclude();
     $includes = $this->resolveVariants($includes);
     //resolve inclusions
     foreach ($includes as $include) {
         if (AssetRegistry::hasNamedAsset($include)) {
             $asset = AssetRegistry::getAsset($include);
             if ($asset instanceof AssetBundle) {
                 foreach ($asset->getAssets() as $bundleAsset) {
                     $this->assets[$bundleAsset->getName()] = clone $bundleAsset;
                 }
             } else {
                 $this->assets[$asset->getName()] = clone $asset;
             }
         } else {
             $name = 'asset' . mt_rand(1111, 99999999);
             $this->assets[$name] = AssetFactory::asset($name, $include);
         }
     }
 }