Skip to content

Commit

Permalink
Replaced Hooks with EventEmitter trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
peteboere committed Sep 2, 2017
1 parent 689ee46 commit 60e89ee
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 100 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -4,4 +4,3 @@ phpunit.xml
composer.lock
vendor
node_modules
package-lock.json
32 changes: 28 additions & 4 deletions README.md
Expand Up @@ -17,8 +17,9 @@ CSS-Crush is a standards inspired preprocessor designed to enable a modern and u

See the [docs](http://the-echoplex.net/csscrush) for full details.

********************************

## Setup
## Setup (PHP)

If you're using [Composer](http://getcomposer.org) you can use Crush in your project with the following line in your terminal:

Expand All @@ -32,8 +33,7 @@ If you're not using Composer yet just download the library into a convenient loc
<?php require_once 'path/to/CssCrush.php'; ?>
```


## Basic usage
## Basic usage (PHP)

```php
<?php
Expand All @@ -57,6 +57,31 @@ There are several other [functions](http://the-echoplex.net/csscrush#api) for wo

There are a number of [options](http://the-echoplex.net/csscrush#api--options) available for tailoring the output, and a collection of bundled [plugins](http://the-echoplex.net/csscrush#plugins) that cover many workflow issues in contemporary CSS development.

********************************

## Setup (JS)

```shell
npm install csscrush
```

## Basic usage (JS)

```js
// All methods can take the standard options (camelCase) as the second argument.
const csscrush = require('csscrush');

// Compile. Returns promise.
csscrush.file('./styles.css', {sourceMap: true});

// Compile string of CSS. Returns promise.
csscrush.string('* {box-sizing: border-box;}');

// Compile and watch file. Returns event emitter (triggers 'data' on compile).
csscrush.watch('./styles.css');
```

********************************

## Contributing

Expand All @@ -70,4 +95,3 @@ Likewise, if you'd like to request a feature please create an [issue](https://gi
## Licence

MIT

2 changes: 1 addition & 1 deletion lib/CssCrush/Declaration.php
Expand Up @@ -44,7 +44,7 @@ public function __construct($property, $value, $contextIndex = 0)
$this->important = true;
}

Crush::$process->hooks->run('declaration_preprocess', array('property' => &$property, 'value' => &$value));
Crush::$process->emit('declaration_preprocess', array('property' => &$property, 'value' => &$value));

// Reject declarations with empty CSS values.
if ($value === false || $value === '') {
Expand Down
2 changes: 1 addition & 1 deletion lib/CssCrush/DeclarationList.php
Expand Up @@ -406,7 +406,7 @@ public static function parse($str, $options = array())
}

if ($options['apply_hooks']) {
Crush::$process->hooks->run('declaration_preprocess', array(
Crush::$process->emit('declaration_preprocess', array(
'property' => &$property,
'value' => &$value,
));
Expand Down
36 changes: 36 additions & 0 deletions lib/CssCrush/EventEmitter.php
@@ -0,0 +1,36 @@
<?php
/**
*
* Event Emitter trait.
*
*/
namespace CssCrush;

trait EventEmitter {

private $eventEmitterStorage = [];
private $eventEmitterUid = 0;

public function on($event, callable $function)
{
if (! isset($this->eventEmitterStorage[$event])) {
$this->eventEmitterStorage[$event] = [];
}

$id = ++$this->eventEmitterUid;
$this->eventEmitterStorage[$event][$id] = $function;

return function () use ($event, $id) {
unset($this->eventEmitterStorage[$event][$id]);
};
}

public function emit($event, $data = null)
{
if (isset($this->eventEmitterStorage[$event])) {
foreach ($this->eventEmitterStorage[$event] as $function) {
$function($data);
}
}
}
}
40 changes: 0 additions & 40 deletions lib/CssCrush/Hooks.php

This file was deleted.

9 changes: 3 additions & 6 deletions lib/CssCrush/IO.php
Expand Up @@ -190,8 +190,7 @@ public function saveCacheData()

debug('Saving config.');

$flags = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
Util::filePutContents($process->cacheFile, json_encode($process->cacheData, $flags), __METHOD__);
Util::filePutContents($process->cacheFile, json_encode($process->cacheData, JSON_PRETTY_PRINT), __METHOD__);
}

public function write(StringObject $string)
Expand All @@ -208,19 +207,17 @@ public function write(StringObject $string)

if (Util::filePutContents("$dir/$filename", $string, __METHOD__)) {

$jsonFlags = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;

if ($process->sourceMap) {
Util::filePutContents("$dir/$sourcemapFilename",
json_encode($process->sourceMap, $jsonFlags), __METHOD__);
json_encode($process->sourceMap, JSON_PRETTY_PRINT), __METHOD__);
}

if ($process->options->stat_dump) {
$statFile = is_string($process->options->stat_dump) ?
$process->options->stat_dump : "$dir/$filename.json";

$GLOBALS['CSSCRUSH_STAT_FILE'] = $statFile;
Util::filePutContents($statFile, json_encode(csscrush_stat(), $jsonFlags), __METHOD__);
Util::filePutContents($statFile, json_encode(csscrush_stat(), JSON_PRETTY_PRINT), __METHOD__);
}

return true;
Expand Down
15 changes: 8 additions & 7 deletions lib/CssCrush/Process.php
Expand Up @@ -8,6 +8,8 @@

class Process
{
use EventEmitter;

public function __construct($user_options = array(), $context = array())
{
$config = Crush::$config;
Expand All @@ -30,7 +32,6 @@ public function __construct($user_options = array(), $context = array())
$this->output = new \stdClass();
$this->tokens = new Tokens();
$this->functions = new Functions();
$this->hooks = new Hooks();
$this->sourceMap = null;
$this->selectorAliases = array();
$this->selectorAliasesPatt = null;
Expand Down Expand Up @@ -713,18 +714,18 @@ protected function processRules()
$rule->declarations->flatten();
$rule->declarations->process();

$this->hooks->run('rule_prealias', $rule);
$this->emit('rule_prealias', $rule);

$rule->declarations->aliasProperties($rule->vendorContext);
$rule->declarations->aliasFunctions($rule->vendorContext);
$rule->declarations->aliasDeclarations($rule->vendorContext);

$this->hooks->run('rule_postalias', $rule);
$this->emit('rule_postalias', $rule);

$rule->selectors->expand();
$rule->applyExtendables();

$this->hooks->run('rule_postprocess', $rule);
$this->emit('rule_postprocess', $rule);
}
}

Expand Down Expand Up @@ -957,7 +958,7 @@ public function compile()
$this->string = new StringObject($importer->collate());

// Capture phase 0 hook: Before all variables and settings have resolved.
$this->hooks->run('capture_phase0', $this);
$this->emit('capture_phase0', $this);

$this->captureVars();

Expand All @@ -970,7 +971,7 @@ public function compile()
$this->resolveLoops();

// Capture phase 1 hook: After all variables and settings have resolved.
$this->hooks->run('capture_phase1', $this);
$this->emit('capture_phase1', $this);

$this->resolveSelectorAliases();

Expand All @@ -979,7 +980,7 @@ public function compile()
$this->resolveFragments();

// Capture phase 2 hook: After most built-in directives have resolved.
$this->hooks->run('capture_phase2', $this);
$this->emit('capture_phase2', $this);

$this->captureRules();

Expand Down
2 changes: 1 addition & 1 deletion plugins/canvas.php
Expand Up @@ -10,7 +10,7 @@

Plugin::register('canvas', array(
'enable' => function ($process) {
$process->hooks->add('capture_phase2', 'CssCrush\canvas_capture');
$process->on('capture_phase2', 'CssCrush\canvas_capture');
$process->functions->add('canvas', 'CssCrush\canvas_generator');
$process->functions->add('canvas-data', 'CssCrush\canvas_generator');
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/color.php
Expand Up @@ -9,8 +9,8 @@
Plugin::register('color', array(
'enable' => function ($process) {
$GLOBALS['CSSCRUSH_COLOR_PATT'] = null;
$process->hooks->add('capture_phase1', 'CssCrush\color_capture');
$process->hooks->add('declaration_preprocess', 'CssCrush\color');
$process->on('capture_phase1', 'CssCrush\color_capture');
$process->on('declaration_preprocess', 'CssCrush\color');
}
));

Expand Down
2 changes: 1 addition & 1 deletion plugins/ease.php
Expand Up @@ -8,7 +8,7 @@

Plugin::register('ease', array(
'enable' => function ($process) {
$process->hooks->add('rule_prealias', 'CssCrush\ease');
$process->on('rule_prealias', 'CssCrush\ease');
}
));

Expand Down
2 changes: 1 addition & 1 deletion plugins/property-sorter.php
Expand Up @@ -8,7 +8,7 @@

Plugin::register('property-sorter', array(
'enable' => function ($process) {
$process->hooks->add('rule_prealias', 'CssCrush\property_sorter');
$process->on('rule_prealias', 'CssCrush\property_sorter');
}
));

Expand Down
2 changes: 1 addition & 1 deletion plugins/rem.php
Expand Up @@ -8,7 +8,7 @@

Plugin::register('rem', array(
'enable' => function ($process) {
$process->hooks->add('rule_prealias', 'CssCrush\rem');
$process->on('rule_prealias', 'CssCrush\rem');
}
));

Expand Down
2 changes: 1 addition & 1 deletion plugins/svg.php
Expand Up @@ -8,7 +8,7 @@

Plugin::register('svg', array(
'enable' => function ($process) {
$process->hooks->add('capture_phase2', 'CssCrush\svg_capture');
$process->on('capture_phase2', 'CssCrush\svg_capture');
$process->functions->add('svg', 'CssCrush\fn__svg');
$process->functions->add('svg-data', 'CssCrush\fn__svg_data');
$process->functions->add('svg-linear-gradient', 'CssCrush\fn__svg_linear_gradient');
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/CssCrush/EventEmitterTest.php
@@ -0,0 +1,32 @@
<?php

namespace CssCrush\UnitTest;

use CssCrush\EventEmitter;

class EventEmitterTest extends \PHPUnit_Framework_TestCase
{
public function testAll()
{
$emitter = new EventEmitterHost();

$foo = null;
$cancelEvent = $emitter->on('foo', function ($data) use (&$foo) {
$foo = $data;
});

$this->assertEquals($foo, null);

$emitter->emit('foo', 10);

$this->assertEquals($foo, 10);

$cancelEvent();

$emitter->emit('foo', 20);

$this->assertEquals($foo, 10);
}
}

class EventEmitterHost { use EventEmitter; }
33 changes: 0 additions & 33 deletions tests/unit/CssCrush/HooksTest.php

This file was deleted.

0 comments on commit 60e89ee

Please sign in to comment.