1: <?php
2:
3: namespace WebLoader\Nette;
4:
5: use WebLoader\Compiler;
6: use WebLoader\FileCollection;
7:
8: /**
9: * Web loader
10: *
11: * @author Jan Marek
12: * @license MIT
13: */
14: abstract class WebLoader extends \Nette\Application\UI\Control
15: {
16:
17: /** @var \WebLoader\Compiler */
18: private $compiler;
19:
20: /** @var string */
21: private $tempPath;
22:
23: public function __construct(Compiler $compiler, $tempPath)
24: {
25: parent::__construct();
26: $this->compiler = $compiler;
27: $this->tempPath = $tempPath;
28: }
29:
30: /**
31: * @return \WebLoader\Compiler
32: */
33: public function getCompiler()
34: {
35: return $this->compiler;
36: }
37:
38: /**
39: * @param \WebLoader\Compiler
40: */
41: public function setCompiler(Compiler $compiler)
42: {
43: $this->compiler = $compiler;
44: }
45:
46: /**
47: * @return string
48: */
49: public function getTempPath()
50: {
51: return $this->tempPath;
52: }
53:
54: /**
55: * @param string
56: */
57: public function setTempPath($tempPath)
58: {
59: $this->tempPath = $tempPath;
60: }
61:
62: /**
63: * Get html element including generated content
64: * @param string $source
65: * @return \Nette\Utils\Html
66: */
67: abstract public function getElement($source);
68:
69: /**
70: * Generate compiled file(s) and render link(s)
71: */
72: public function render()
73: {
74: $hasArgs = func_num_args() > 0;
75:
76: if ($hasArgs) {
77: $backup = $this->compiler->getFileCollection();
78: $newFiles = new FileCollection($backup->getRoot());
79: $newFiles->addFiles(func_get_args());
80: $this->compiler->setFileCollection($newFiles);
81: }
82:
83: // remote files
84: foreach ($this->compiler->getFileCollection()->getRemoteFiles() as $file) {
85: echo $this->getElement($file), PHP_EOL;
86: }
87:
88: foreach ($this->compiler->generate() as $file) {
89: echo $this->getElement($this->getGeneratedFilePath($file)), PHP_EOL;
90: }
91:
92: if ($hasArgs) {
93: $this->compiler->setFileCollection($backup);
94: }
95: }
96:
97: protected function getGeneratedFilePath($file)
98: {
99: return $this->tempPath . '/' . $file->file . '?' . $file->lastModified;
100: }
101:
102: }
103: