HosipLan

assets

Jul 23rd, 2011
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.81 KB | None | 0 0
  1. <?php
  2.  
  3. namespace WOW\Service;
  4.  
  5. use Nette\Application\UI\Presenter;
  6. use Nette\DI\Container;
  7. use Nette\Utils\Finder;
  8. use Nette\Caching\Cache;
  9. use Nette\Reflection\ClassType;
  10.  
  11.  
  12.  
  13. class ComponentRegister extends Nette\Object
  14. {  
  15.     /** @var Cache */
  16.     private $cache;
  17.  
  18.     /** @var array */
  19.     private $types = array(
  20.             'jpg' => '%wwwDir%/images',
  21.             'png' => '%wwwDir%/images',
  22.             'gif' => '%wwwDir%/images',
  23.             'css' => '%wwwDir%/css',
  24.             'js' => '%wwwDir%/js',
  25.         );
  26.  
  27.  
  28.  
  29.     /**
  30.      * @param Nette\Caching\IStorage $cacheStorage
  31.      */
  32.     public function __construct(Nette\Caching\IStorage $cacheStorage)
  33.     {
  34.         $this->cache = new Cache($cacheStorage, 'WOW.ComponentInstaller');
  35.     }
  36.  
  37.  
  38.  
  39.     /**
  40.      * @param Presenter $presenter
  41.      */
  42.     public function installPresenterComponents(Presenter $presenter)
  43.     {
  44.         if ($this->isInstalled($presenter)) {
  45.             return;
  46.         }
  47.  
  48.         $files = array();
  49.         foreach ($this->getPresenterComponents($presenter) as $component) {
  50.             $files = array_merge($files, $this->prepareAssets($component, $presenter->getContext()));
  51.         }
  52.  
  53.         $this->setInstalled($presenter, $files);
  54.     }
  55.  
  56.  
  57.  
  58.     /**
  59.      * @param Presenter $presenters
  60.      * @return array of files
  61.      */
  62.     public function getPresenterAssets(Presenter $presenter)
  63.     {
  64.         if (!$this->isInstalled($presenter)) {
  65.             throw new Nette\InvalidArgumentException("Presenter " . get_class($presenter) . " is not installed");
  66.         }
  67.        
  68.         return $this->cache->load($presenter->getAction(TRUE));
  69.     }
  70.  
  71.  
  72.  
  73.     /**
  74.      * @param Presenter $presenters
  75.      * @return boolean
  76.      */
  77.     public function isInstalled(Presenter $presenter)
  78.     {
  79.         return $this->cache->load($presenter->getAction(TRUE)) !== NULL;
  80.     }
  81.  
  82.  
  83.  
  84.     /**
  85.      * @param Presenter $presenters
  86.      * @param array $files
  87.      */
  88.     private function setInstalled(Presenter $presenter, array $files)
  89.     {
  90.         $this->cache->save(
  91.                 $presenter->getAction(TRUE), // vrací "nette" cestu k presenteru
  92.                 $files, // list of files to include, Control will resolve...
  93.                 array(
  94.                         Cache::FILES => $files + array('presenter' => $presenter->getReflection()->getFileName())
  95.                     )
  96.             );
  97.     }
  98.  
  99.  
  100.  
  101.     /**
  102.      * @param object $component
  103.      * @param Container $context
  104.      * @return array of sources
  105.      */
  106.     private function prepareAssets($component, Container $context)
  107.     {
  108.         $files = array();
  109.         foreach ($this->types as $type => $publicPath) {
  110.             foreach (Finder::findFiles('*.' . $type)->from($component->path) as $file) {
  111.                 $files[] = $source = $file->getRealPath();
  112.                 @copy($source, $context->expand($publicPath . '/' . basename($source)));
  113.             }
  114.         }
  115.        
  116.         return $files;
  117.     }
  118.  
  119.  
  120.  
  121.     /**
  122.      * @param Presenter $presenter
  123.      * @return array components
  124.      */
  125.     private function getPresenterComponents(Presenter $presenter)
  126.     {
  127.         $components = array();
  128.         foreach (ClassType::from($presenter)->getMethods() as $method) {
  129.             if (substr($method->name, 0, 15) !== 'createComponent') {
  130.                 continue;
  131.             }
  132.  
  133.             if ($method->hasAnnotation('component')) {
  134.                 if (!$method->getAnnotation('return')) {
  135.                     throw new Nette\UnexpectedValueException("Set annotation @return for " . $method->getName() . " in " . get_class($presenter));
  136.                 }
  137.  
  138.                 $name = lcFirst(substr($method->getName(), 15));
  139.                 $components[] = (object)array(
  140.                         'name' => $name,
  141.                         'factory' => $method,
  142.                         'class' => $reflection->getAnnotation('return'),
  143.                         'path' => ClassType::from($reflection->getAnnotation('return'))->getFileName();
  144.                     );
  145.             }
  146.         }
  147.  
  148.         return $components;
  149.     }
  150.  
  151. }
  152.  
  153.  
  154.  
  155. abstract class BasePresenter extends Nette\Application\UI\Presenter
  156. {
  157.  
  158.     protected function startup()
  159.     {
  160.         parent::startup();
  161.         $this->context->componentRegister->installPresenterComponents($this);
  162.     }
  163.  
  164.  
  165.  
  166.     /**
  167.      * @return AssetsControl
  168.      */
  169.     protected function createComponentAssets()
  170.     {
  171.         return new AssetsControl($this->context->componentRegister);
  172.     }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment