Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch json_encode errors #14

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
index.php
index.php

# JetBrains IDEs
.idea
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: precise
language: php
php:
- "5.5"
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Autoprefixer PHP [![Build Status](https://travis-ci.org/vladkens/autoprefixer-php.svg?branch=master)](https://travis-ci.org/vladkens/autoprefixer-php)
# Autoprefixer PHP

[Autoprefixer](https://github.com/ai/autoprefixer) is a tool
to parse CSS and add vendor prefixes to CSS rules using values
Expand Down Expand Up @@ -93,11 +93,18 @@ $autoprefixer->compile($css_one, 'last 1 version');
$autoprefixer->update();
```

## Configuration

You can set the dir for the Autoprefixer error log using the const `AF_LOG_DIR`. By default,
the log will be written in the `sys_get_temp_dir()`.

The path to the Node.js can be set using the const `NODE_PATH` or the `node` will be used in the `proc_open`.

## Speed
On my Intel i5-3210M 2.5GHz and HDD 5200 RPM GitHub styles compiled in 390 ms.

## License
[MIT](https://raw.github.com/vladkens/autoprefixer-php/master/LICENSE)

## More docs
See https://github.com/ai/autoprefixer/blob/master/README.md
See https://github.com/ai/autoprefixer/blob/master/README.md
259 changes: 147 additions & 112 deletions lib/Autoprefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,116 +7,151 @@

class Autoprefixer
{
/**
* @var array
*/
private $browsers = array();

/**
* @param mixed $browsers
*/
public function __construct($browsers = null)
{
if (!is_null($browsers)) {
$this->setBrowsers($browsers);
}
}

/**
* Set browsers info.
* @param mixed $browsers String if one argument, array if many.
* @return void
*/
public function setBrowsers($browsers)
{
if (!is_array($browsers)) {
$browsers = array($browsers);
}
$this->browsers = $browsers;
}

/**
* @param mixed $css
* @param mixed $browsers
* @throws RuntimeException If node runtime unavailable
* @throws AutoprefixerException
* @return array
*/
public function compile($css, $browsers = null)
{
if ($return_string = !is_array($css)) {
$css = array($css);
}

$nodejs = proc_open('node ' . __DIR__ . '/vendor/wrap.js',
array(array('pipe', 'r'), array('pipe', 'w')),
$pipes
);
if ($nodejs === false) {
throw new RuntimeException('Could not reach node runtime');
}

$this->fwrite_stream($pipes[0],
json_encode(array(
'css' => $css,
'browsers' => !is_null($browsers) ? $browsers : $this->browsers)
));
fclose($pipes[0]);

$output = stream_get_contents($pipes[1]);
$output = json_decode($output, true);
fclose($pipes[1]);

proc_close($nodejs);

$error_messages = '';
foreach ($output as $key => &$value) {
if (preg_match('/^Error:\s*/i', $value)) {
$value = preg_replace('/^Error:\s*/i', '', $value);
$error_messages .= "In css[$key]: $value \n";
}
}

if (strlen($error_messages) > 0) {
throw new AutoprefixerException($error_messages);
}

return $return_string ? $output[0] : $output;
}

/**
* Download autoprefixer updates.
* @return bool True if updated.
*/
public function update()
{
$update_url = 'https://raw.github.com/ai/autoprefixer-rails/master/vendor/autoprefixer.js';
$local_path = __DIR__ . '/vendor/autoprefixer.js';
$new = file_get_contents($update_url);
$old = file_get_contents($local_path);

if (md5($new) == md5($old)) return false;

file_put_contents($local_path, $new);
return true;
}

/**
* @param object $fp php://stdin
* @param string $string
* @param int $buflen
* @return string
*/
private function fwrite_stream($fp, $string, $buflen = 4096)
{
for ($written = 0, $len = strlen($string); $written < $len; $written += $fwrite) {
$fwrite = fwrite($fp, substr($string, $written, $buflen));
if ($fwrite === false) {
return $written;
}
}

return $written;
}

/**
* @var array
*/
private $browsers = array();

/**
* @param mixed $browsers
*/
public function __construct($browsers = null)
{
if (!is_null($browsers)) {
$this->setBrowsers($browsers);
}
}

/**
* Set browsers info.
* @param mixed $browsers String if one argument, array if many.
* @return void
*/
public function setBrowsers($browsers)
{
if (!is_array($browsers)) {
$browsers = array($browsers);
}
$this->browsers = $browsers;
}

/**
* @param mixed $css
* @param mixed $browsers
* @throws RuntimeException If node runtime unavailable
* @throws AutoprefixerException
* @return array
*/
public function compile($css, $browsers = null)
{
$return_string = !is_array($css);

if ($return_string) {
$css = array($css);
}

$data_string = json_encode(array(
'css' => $css,
'browsers' => !is_null($browsers) ? $browsers : $this->browsers
));

if ($data_string === false || $data_string === null) {
$error_message = 'Failed to json_encode: ';
if (function_exists('json_last_error_msg')) {
$error_message .= json_last_error_msg();
} else {
$error_message .= json_last_error();
}
throw new AutoprefixerException($error_message);
}

// by default, use OS temp dir
$error_log_dir = defined('AF_LOG_DIR') ? AF_LOG_DIR : sys_get_temp_dir();
$error_log_file = $error_log_dir . DIRECTORY_SEPARATOR . 'autoprefixer.error.log';
if (!file_exists($error_log_file)) {
@touch($error_log_file);
}
if (!is_writable($error_log_file)){
throw new AutoprefixerException("Error log file '$error_log_file' isn't writable");
}

$nodejs = proc_open((defined('NODE_PATH') ? NODE_PATH : 'node') . ' ' . __DIR__ . '/vendor/wrap.js',
array(array('pipe', 'r'), array('pipe', 'w'), array('file', $error_log_file, 'a')),
$pipes
);

if ($nodejs === false) {
throw new RuntimeException('Could not reach node runtime');
}

$written = $this->fwrite_stream($pipes[0], $data_string);
fclose($pipes[0]);

if ($written !== strlen($data_string)) {
proc_close($nodejs);
throw new AutoprefixerException(sprintf('Could not write data: %d/%d', $written, strlen($data_string)));
}

$output = stream_get_contents($pipes[1]);
proc_close($nodejs);

if (!$output) {
throw new AutoprefixerException('Could not read output');
}
$output = json_decode($output, true);

if (!is_array($output)) {
throw new AutoprefixerException('Broken output');
}

$error_messages = '';
foreach ($output as $key => &$value) {
if (preg_match('/^Error:\s*/i', $value)) {
$value = preg_replace('/^Error:\s*/i', '', $value);
$error_messages .= "In css[$key]: $value \n";
}
}

if (strlen($error_messages) > 0) {
throw new AutoprefixerException($error_messages);
}

return $return_string ? $output[0] : $output;
}

/**
* Download autoprefixer updates.
* @return bool True if updated.
*/
public function update()
{
$update_url = 'https://raw.github.com/ai/autoprefixer-rails/master/vendor/autoprefixer.js';
$local_path = __DIR__ . '/vendor/autoprefixer.js';
$new = file_get_contents($update_url);
$old = file_get_contents($local_path);

if (md5($new) == md5($old)) return false;

file_put_contents($local_path, $new);
return true;
}

/**
* @param object $fp php://stdin
* @param string $string
* @param int $buflen
* @return string
*/
private function fwrite_stream($fp, $string, $buflen = 4096)
{
for ($written = 0, $len = strlen($string); $written < $len; $written += $fwrite) {
$fwrite = fwrite($fp, substr($string, $written, $buflen));
if ($fwrite === false) {
return $written;
}
}

return $written;
}

};
8 changes: 7 additions & 1 deletion test/AutoprefixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ protected function register($name, $path)
}
}

$input = $this->clear($this->autoprefixer->compile($input, $browsers));
try {
$input = $this->autoprefixer->compile($input, $browsers);
} catch (AutoprefixerException $e) {
$output = $input;
}

$input = $this->clear($input);
$output = $this->clear($output);

static::$tests[$name] = array($input, $output);
Expand Down
3 changes: 3 additions & 0 deletions test/cases/utf8.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.comment:before{
content: '“';
}
3 changes: 3 additions & 0 deletions test/cases/utf8_broken.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.square:after {
content: ' ft�';
}