source: sandbox/newExpressoMail/newExpressoMail/Assetic/Filter/GoogleClosure/CompilerApiFilter.php @ 7265

Revision 7265, 3.6 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\GoogleClosure;
13
14use Assetic\Asset\AssetInterface;
15
16/**
17 * Filter for the Google Closure Compiler API.
18 *
19 * @link https://developers.google.com/closure/compiler/
20 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
21 */
22class CompilerApiFilter extends BaseCompilerFilter
23{
24    public function filterDump(AssetInterface $asset)
25    {
26        $query = array(
27            'js_code'       => $asset->getContent(),
28            'output_format' => 'json',
29            'output_info'   => 'compiled_code',
30        );
31
32        if (null !== $this->compilationLevel) {
33            $query['compilation_level'] = $this->compilationLevel;
34        }
35
36        if (null !== $this->jsExterns) {
37            $query['js_externs'] = $this->jsExterns;
38        }
39
40        if (null !== $this->externsUrl) {
41            $query['externs_url'] = $this->externsUrl;
42        }
43
44        if (null !== $this->excludeDefaultExterns) {
45            $query['exclude_default_externs'] = $this->excludeDefaultExterns ? 'true' : 'false';
46        }
47
48        if (null !== $this->formatting) {
49            $query['formatting'] = $this->formatting;
50        }
51
52        if (null !== $this->useClosureLibrary) {
53            $query['use_closure_library'] = $this->useClosureLibrary ? 'true' : 'false';
54        }
55
56        if (null !== $this->warningLevel) {
57            $query['warning_level'] = $this->warningLevel;
58        }
59
60        if (null !== $this->language) {
61            $query['language'] = $this->language;
62        }
63
64        if (preg_match('/1|yes|on|true/i', ini_get('allow_url_fopen'))) {
65            $context = stream_context_create(array('http' => array(
66                'method'  => 'POST',
67                'header'  => 'Content-Type: application/x-www-form-urlencoded',
68                'content' => http_build_query($query),
69            )));
70
71            $response = file_get_contents('http://closure-compiler.appspot.com/compile', false, $context);
72            $data = json_decode($response);
73
74         } elseif (defined('CURLOPT_POST') && !in_array('curl_init', explode(',', ini_get('disable_functions')))) {
75
76            $ch = curl_init('http://closure-compiler.appspot.com/compile');
77            curl_setopt($ch, CURLOPT_POST, true);
78            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
79            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
80            curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
81            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
82            $response = curl_exec($ch);
83            curl_close($ch);
84
85            $data = json_decode($response);
86        } else {
87            throw new \RuntimeException("There is no known way to contact closure compiler available");
88        }
89
90        if (isset($data->serverErrors) && 0 < count($data->serverErrors)) {
91            // @codeCoverageIgnoreStart
92            throw new \RuntimeException(sprintf('The Google Closure Compiler API threw some server errors: '.print_r($data->serverErrors, true)));
93            // @codeCoverageIgnoreEnd
94        }
95
96        if (isset($data->errors) && 0 < count($data->errors)) {
97            // @codeCoverageIgnoreStart
98            throw new \RuntimeException(sprintf('The Google Closure Compiler API threw some errors: '.print_r($data->errors, true)));
99            // @codeCoverageIgnoreEnd
100        }
101
102        $asset->setContent($data->compiledCode);
103    }
104}
Note: See TracBrowser for help on using the repository browser.