source: sandbox/newExpressoMail/newExpressoMail/Assetic/Filter/HandlebarsFilter.php @ 7265

Revision 7265, 2.8 KB checked in by gustavo, 12 years ago (diff)

Ticket #0000 - Criado novo modulo para o desenvolvimento do novo ExpressoMail?

  • Property svn:executable set to *
Line 
1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2012 OpenSky Project Inc
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Assetic\Filter;
13
14use Assetic\Asset\AssetInterface;
15use Assetic\Exception\FilterException;
16use Symfony\Component\Process\ProcessBuilder;
17
18/**
19 * Compiles Handlebars templates into Javascript.
20 *
21 * @link http://handlebarsjs.com/
22 * @author Keyvan Akbary <keyvan@funddy.com>
23 */
24class HandlebarsFilter implements FilterInterface
25{
26    private $handlebarsPath;
27    private $nodePath;
28
29    private $minimize = false;
30    private $simple = false;
31
32    public function __construct($handlebarsPath = '/usr/bin/handlebars', $nodePath = null)
33    {
34        $this->handlebarsPath = $handlebarsPath;
35        $this->nodePath = $nodePath;
36    }
37
38    public function setMinimize($minimize)
39    {
40        $this->minimize = $minimize;
41    }
42
43    public function setSimple($simple)
44    {
45        $this->simple = $simple;
46    }
47
48    public function filterLoad(AssetInterface $asset)
49    {
50        $executables = array();
51
52        if ($this->nodePath !== null) {
53            $executables[] = $this->nodePath;
54        }
55
56        $executables[] = $this->handlebarsPath;
57
58        $processBuilder = new ProcessBuilder($executables);
59
60        $templateName = basename($asset->getSourcePath());
61
62        $inputDirPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.uniqid('input_dir');
63        $inputPath = $inputDirPath.DIRECTORY_SEPARATOR.$templateName;
64        $outputPath = tempnam(sys_get_temp_dir(), 'output');
65
66        mkdir($inputDirPath);
67        file_put_contents($inputPath, $asset->getContent());
68
69        $processBuilder->add($inputPath)->add('-f')->add($outputPath);
70
71        if ($this->minimize) {
72            $processBuilder->add('--min');
73        }
74
75        if ($this->simple) {
76            $processBuilder->add('--simple');
77        }
78
79        $process = $processBuilder->getProcess();
80        $returnCode = $process->run();
81
82        unlink($inputPath);
83        rmdir($inputDirPath);
84
85        if (127 === $returnCode) {
86            throw new \RuntimeException('Path to node executable could not be resolved.');
87        }
88
89        if (0 < $returnCode) {
90            if (file_exists($outputPath)) {
91                unlink($outputPath);
92            }
93            throw FilterException::fromProcess($process)->setInput($asset->getContent());
94        }
95
96        if (!file_exists($outputPath)) {
97            throw new \RuntimeException('Error creating output file.');
98        }
99
100        $compiledJs = file_get_contents($outputPath);
101        unlink($outputPath);
102
103        $asset->setContent($compiledJs);
104    }
105
106    public function filterDump(AssetInterface $asset)
107    {
108    }
109}
Note: See TracBrowser for help on using the repository browser.