HosipLan

Untitled

Jul 18th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2. namespace WOW\Service;
  3.  
  4. use Nette\Object;
  5. use Nette\DI\Container;
  6. use Nette\Utils\Finder;
  7. use Nette\Loaders\RobotLoader;
  8. use Nette\InvalidArgumentException;
  9. use Nette\FatalErrorException;
  10. use Nette\IOException;
  11.  
  12. class ComponentInstaller extends Object
  13. {  
  14.  
  15.     public $fileTypes = array();
  16.    
  17.     private $container;
  18.  
  19.     private $files;
  20.  
  21.     private $template;
  22.    
  23.  
  24.  
  25.     // komentáře se mi doplňovat nechce ...
  26.     public function __construct(Container $container)
  27.     {
  28.         $this->container = new Container();
  29.         $this->container->params['wwwDir'] = $container->params['wwwDir'];
  30.         $this->container->params['appDir'] = $container->params['appDir'];
  31.         $this->container->params['productionMode'] = $container->params['productionMode'];
  32.         $this->container->freeze();
  33.        
  34.         $this->addFileType('jpg', '/images');
  35.         $this->addFileType('png', '/images');
  36.         $this->addFileType('gif', '/images');
  37.         $this->addFileType('css', '/css');
  38.         $this->addFileType('js', '/js');
  39.        
  40.         $this->setTemplate('/app/templates/@layout.latte');
  41.     }
  42.  
  43.  
  44.    
  45.     public function addFileType($type, $relativePath)
  46.     {  
  47.         array_push($this->fileTypes, $type);
  48.         $this->container->params[$type] = $this->container->params['wwwDir'] . $relativePath;
  49.     }
  50.  
  51.  
  52.    
  53.     public function setTemplate($templatePath)
  54.     {
  55.         // tohle je dost WTF
  56.         $template = dirname($this->container->params['wwwDir']) . '/' . $templatePath;
  57.        
  58.         if (!is_file($template)) {
  59.             throw new IOException("$template not found.");
  60.         }
  61.  
  62.         $this->template = $template;
  63.     }
  64.  
  65.  
  66.        
  67.     public function install(IComponent $class)
  68.     {
  69.         if ($this->isProduction()) {
  70.             return;
  71.         } // zbytečně uzavíráš velký kus kódu do závorek a odsazuješ ho
  72.  
  73.         $className = $class->reflection->name;
  74.         $classes = $this->getClasses();
  75.         // na určení, kde je třída definována ti stačí reflexe, nemuší obtěžovat robotloader
  76.  
  77.         $this->container->params['sourcePath'] = $this->getPath($classes[$className]);
  78.         $this->files = $this->findFiles();
  79.  
  80.         // tohle použití nedává moc smysl
  81.         $this->fileCopy();
  82.         $this->createTemplateLink();
  83.     }
  84.  
  85.  
  86.    
  87.     private function isProduction()
  88.     {
  89.         return $this->container->params['productionMode'];
  90.     }
  91.  
  92.  
  93.    
  94.     private function findFiles()
  95.     {
  96.         $files = array();
  97.  
  98.         foreach ($this->fileTypes as $key => $type) {
  99.             $fileTypes[$key] = '*.' . $type;
  100.         }
  101.        
  102.         foreach (Finder::findFiles($fileTypes)->from($this->container->params['sourcePath']) as $file) {
  103.             $files[] = $file->getRealPath();
  104.         } // zbytečně definované proměnné
  105.        
  106.         foreach($this->fileTypes as $type) {
  107.             $reg = "/\.$type$/";
  108.             foreach($files as $file) {
  109.                 if(preg_match($reg, $file)) {
  110.                     $sortedFiles[$type][] = $file;
  111.                 }
  112.             }
  113.         }
  114.  
  115.         return $sortedFiles;
  116.     }
  117.  
  118.  
  119.    
  120.     private function fileCopy()
  121.     {
  122.         foreach ($this->files as $fileType => $files) {
  123.             foreach ($files as $source) {
  124.                 $target = $this->container->params[$fileType];
  125.                 copy($source, $target . '/' . basename($source));
  126.             } // zbytečně definované proměnné
  127.         }
  128.     }
  129.  
  130.  
  131.    
  132.     private function createTemplateLink()
  133.     {
  134.         //todo
  135.         $template = file_get_contents($this->template);
  136.        
  137.     }
  138.            
  139.  
  140.    
  141.     private function getPath($path)
  142.     {
  143.         $path = explode('\\', $path);
  144.         end($path);
  145.         unset($path[key($path)]);
  146.         return implode('\\', $path); // zbytečně definované proměnné
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment