Overview

Namespaces

  • Budovy
  • Kdyby
    • BootstrapFormRenderer
      • DI
      • Latte
  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • Nextras
    • Datagrid
  • None
  • PHP
  • Tester
    • CodeCoverage
    • Runner
      • Output
  • Vodacek
    • Forms
      • Controls
  • WebLoader
    • Filter
    • Nette

Classes

  • Compiler
  • DefaultOutputNamingConvention
  • FileCollection
  • LoaderFactory
  • Path

Interfaces

  • IFileCollection
  • IOutputNamingConvention

Exceptions

  • FileNotFoundException
  • InvalidArgumentException
  • WebLoaderException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace WebLoader;
  4: 
  5: /**
  6:  * Compiler
  7:  *
  8:  * @author Jan Marek
  9:  */
 10: class Compiler
 11: {
 12: 
 13:     /** @var string */
 14:     private $outputDir;
 15: 
 16:     /** @var bool */
 17:     private $joinFiles = true;
 18: 
 19:     /** @var array */
 20:     private $filters = array();
 21: 
 22:     /** @var array */
 23:     private $fileFilters = array();
 24: 
 25:     /** @var IFileCollection */
 26:     private $collection;
 27: 
 28:     /** @var IOutputNamingConvention */
 29:     private $namingConvention;
 30: 
 31:     /** @var bool */
 32:     private $checkLastModified = true;
 33: 
 34:     public function __construct(IFileCollection $files, IOutputNamingConvention $convention, $outputDir)
 35:     {
 36:         $this->collection = $files;
 37:         $this->namingConvention = $convention;
 38:         $this->setOutputDir($outputDir);
 39:     }
 40: 
 41:     /**
 42:      * Create compiler with predefined css output naming convention
 43:      * @param IFileCollection $files
 44:      * @param string $outputDir
 45:      * @return Compiler
 46:      */
 47:     public static function createCssCompiler(IFileCollection $files, $outputDir)
 48:     {
 49:         return new static($files, DefaultOutputNamingConvention::createCssConvention(), $outputDir);
 50:     }
 51: 
 52:     /**
 53:      * Create compiler with predefined javascript output naming convention
 54:      * @param IFileCollection $files
 55:      * @param string $outputDir
 56:      * @return Compiler
 57:      */
 58:     public static function createJsCompiler(IFileCollection $files, $outputDir)
 59:     {
 60:         return new static($files, DefaultOutputNamingConvention::createJsConvention(), $outputDir);
 61:     }
 62: 
 63:     /**
 64:      * Get temp path
 65:      * @return string
 66:      */
 67:     public function getOutputDir()
 68:     {
 69:         return $this->outputDir;
 70:     }
 71: 
 72:     /**
 73:      * Set temp path
 74:      * @param string $tempPath
 75:      */
 76:     public function setOutputDir($tempPath)
 77:     {
 78:         $tempPath = Path::normalize($tempPath);
 79: 
 80:         if (!is_dir($tempPath)) {
 81:             throw new FileNotFoundException('Temp path does not exist.');
 82:         }
 83: 
 84:         if (!is_writable($tempPath)) {
 85:             throw new InvalidArgumentException("Directory '$tempPath' is not writeable.");
 86:         }
 87: 
 88:         $this->outputDir = $tempPath;
 89:     }
 90: 
 91:     /**
 92:      * Get join files
 93:      * @return bool
 94:      */
 95:     public function getJoinFiles()
 96:     {
 97:         return $this->joinFiles;
 98:     }
 99: 
100:     /**
101:      * Set join files
102:      * @param bool $joinFiles
103:      */
104:     public function setJoinFiles($joinFiles)
105:     {
106:         $this->joinFiles = (bool) $joinFiles;
107:     }
108: 
109:     /**
110:      * Set check last modified
111:      * @param bool $checkLastModified
112:      */
113:     public function setCheckLastModified($checkLastModified)
114:     {
115:         $this->checkLastModified = (bool) $checkLastModified;
116:     }
117: 
118:     /**
119:      * Get last modified timestamp of newest file
120:      * @param array $files
121:      * @return int
122:      */
123:     public function getLastModified(array $files = null)
124:     {
125:         if ($files === null) {
126:             $files = $this->collection->getFiles();
127:         }
128: 
129:         $modified = 0;
130: 
131:         foreach ($files as $file) {
132:             $modified = max($modified, filemtime($file));
133:         }
134: 
135:         return $modified;
136:     }
137: 
138:     /**
139:      * Get joined content of all files
140:      * @param array $files
141:      * @return string
142:      */
143:     public function getContent(array $files = null)
144:     {
145:         if ($files === null) {
146:             $files = $this->collection->getFiles();
147:         }
148: 
149:         // load content
150:         $content = '';
151:         foreach ($files as $file) {
152:             $content .= PHP_EOL . $this->loadFile($file);
153:         }
154: 
155:         // apply filters
156:         foreach ($this->filters as $filter) {
157:             $content = call_user_func($filter, $content, $this);
158:         }
159: 
160:         return $content;
161:     }
162: 
163:     /**
164:      * Load content and save file
165:      * @param bool $ifModified
166:      * @return array filenames of generated files
167:      */
168:     public function generate($ifModified = TRUE)
169:     {
170:         if ($this->joinFiles) {
171:             return array(
172:                 $this->generateFiles($this->collection->getFiles(), $ifModified)
173:             );
174:         } else {
175:             $arr = array();
176: 
177:             foreach ($this->collection->getFiles() as $file) {
178:                 $arr[] = $this->generateFiles(array($file), $ifModified);
179:             }
180: 
181:             return $arr;
182:         }
183:     }
184: 
185:     protected function generateFiles(array $files, $ifModified)
186:     {
187:         $name = $this->namingConvention->getFilename($files, $this);
188:         $path = $this->outputDir . '/' . $name;
189:         $lastModified = $this->checkLastModified ? $this->getLastModified($files) : 0;
190: 
191:         if (!$ifModified || !file_exists($path) || $lastModified > filemtime($path)) {
192:             $outPath = in_array('safe', stream_get_wrappers()) ? 'safe://' . $path : $path;
193:             file_put_contents($outPath, $this->getContent($files));
194:         }
195: 
196:         return (object) array(
197:             'file' => $name,
198:             'lastModified' => $lastModified,
199:             'sourceFiles' => $files,
200:         );
201:     }
202: 
203:     /**
204:      * Load file
205:      * @param string $file path
206:      * @return string
207:      */
208:     protected function loadFile($file)
209:     {
210:         $content = file_get_contents($file);
211: 
212:         foreach ($this->fileFilters as $filter) {
213:             $content = call_user_func($filter, $content, $this, $file);
214:         }
215: 
216:         return $content;
217:     }
218: 
219:     /**
220:      * @return \WebLoader\IFileCollection
221:      */
222:     public function getFileCollection()
223:     {
224:         return $this->collection;
225:     }
226: 
227:     /**
228:      * @return \WebLoader\IOutputNamingConvention
229:      */
230:     public function getOutputNamingConvention()
231:     {
232:         return $this->namingConvention;
233:     }
234: 
235:     /**
236:      * @param \WebLoader\IFileCollection $collection
237:      */
238:     public function setFileCollection(IFileCollection $collection)
239:     {
240:         $this->collection = $collection;
241:     }
242: 
243:     /**
244:      * @param \WebLoader\IOutputNamingConvention $namingConvention
245:      */
246:     public function setOutputNamingConvention(IOutputNamingConvention $namingConvention)
247:     {
248:         $this->namingConvention = $namingConvention;
249:     }
250: 
251:     /**
252:      * @param callback $filter
253:      * @throws InvalidArgumentException
254:      */
255:     public function addFilter($filter)
256:     {
257:         if (!is_callable($filter)) {
258:             throw new InvalidArgumentException('Filter is not callable.');
259:         }
260: 
261:         $this->filters[] = $filter;
262:     }
263: 
264:     /**
265:      * @return array
266:      */
267:     public function getFilters()
268:     {
269:         return $this->filters;
270:     }
271: 
272:     /**
273:      * @param callback $filter
274:      * @throws InvalidArgumentException
275:      */
276:     public function addFileFilter($filter)
277:     {
278:         if (!is_callable($filter)) {
279:             throw new InvalidArgumentException('Filter is not callable.');
280:         }
281: 
282:         $this->fileFilters[] = $filter;
283:     }
284: 
285:     /**
286:      * @return array
287:      */
288:     public function getFileFilters()
289:     {
290:         return $this->fileFilters;
291:     }
292: 
293: }
294: 
API documentation generated by ApiGen 2.8.0