Skip to content
This repository has been archived by the owner on Mar 19, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ceesvanegmond committed Apr 16, 2014
2 parents 6469fe9 + 8719245 commit fd72081
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 35 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ You can use this Facade anywhere in your application
{{ Minify::stylesheet('/css/main.css') }}
//or by passing multiple files
{{ Minify::stylesheet(array('/css/main.css', '/css/bootstrap.css')) }}
//add custom attributes
{{ Minify::stylesheet(array('/css/main.css', '/css/bootstrap.css'), array('foo' => 'bar')) }}
</head>
...
</html>
Expand All @@ -60,6 +62,8 @@ You can use this Facade anywhere in your application
{{ Minify::javascript('/js/jquery.js') }}
//or by passing multiple files
{{ Minify::javascript(array('/js/jquery.js', '/js/jquery-ui.js')) }}
//add custom attributes
{{ Minify::javascript(array('/js/jquery.js', '/js/jquery-ui.js'), array('bar' => 'baz')) }}
</html>

```
Expand All @@ -79,7 +83,7 @@ return array(
|
*/

'ignore_envionments' => array(
'ignore_environments' => array(
'local',
),

Expand Down Expand Up @@ -119,14 +123,14 @@ return array(
```php
<?php
$config = array(
'ignore_envionments' => 'local',
'ignore_environments' => 'local',
'js_build_path' => '/js/builds/',
'css_builds_path' => '/css/builds',
)
$minify = new CeesVanEgmond\Minify\Providers\Javascript($public_path);
$minify->add($file)

if (in_array($environment, $config['ignore_envionments']))
if (in_array($environment, $config['ignore_environments']))
{
return $provider->tags();
}
Expand Down
12 changes: 12 additions & 0 deletions spec/CeesVanEgmond/Minify/Providers/StyleSheetSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ function it_adds_multiple_files()
$this->shouldHaveCount(2);
}

function it_adds_custom_attributes()
{
$this->tag('file', array('foobar' => 'baz'))
->shouldReturn('<link foobar="baz" href="file" rel="stylesheet">' . PHP_EOL);
}

function it_adds_without_custom_attributes()
{
$this->tag('file')
->shouldReturn('<link href="file" rel="stylesheet">' . PHP_EOL);
}

function it_throws_exception_when_file_not_exists()
{
$this->shouldThrow('CeesVanEgmond\Minify\Exceptions\FileNotExistException')
Expand Down
3 changes: 2 additions & 1 deletion src/CeesVanEgmond/Minify/Contracts/MinifyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public function minify();

/**
* @param $file
* @param $attributes
* @return mixed
*/
public function tag($file);
public function tag($file, array $attributes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php namespace CeesVanEgmond\Minify\Exceptions;

class InvalidArgumentException extends \Exception{}
93 changes: 71 additions & 22 deletions src/CeesVanEgmond/Minify/Minify.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace CeesVanEgmond\Minify;

use CeesVanEgmond\Minify\Exceptions\InvalidArgumentException;
use CeesVanEgmond\Minify\Providers\JavaScript;
use CeesVanEgmond\Minify\Providers\StyleSheet;

Expand All @@ -10,68 +11,116 @@ class Minify
*/
protected $config;

/**
* @var array
*/
protected $attributes = array();

/**
* @var string
*/
private $environment;

/**
* @var
*/
private $provider;

/**
* @var
*/
private $buildPath;

/**
* @param array $config
* @param $environment
* @param string $environment
*/
public function __construct(array $config, $environment)
{
$this->checkConfiguration($config);

$this->config = $config;
$this->environment = $environment;
}

/**
* @param $file
* @param array $attributes
* @return string
*/
public function javascript($file)
public function javascript($file, $attributes = array())
{
$provider = new JavaScript(public_path());
$buildPath = $this->config['js_build_path'];
$this->provider = new JavaScript(public_path());
$this->buildPath = $this->config['js_build_path'];
$this->attributes = $attributes;

$this->process($file);

return $this->process($file, $provider, $buildPath);
return $this;
}

/**
* @param $file
* @param array $attributes
* @return string
*/
public function stylesheet($file)
public function stylesheet($file, $attributes = array())
{
$provider = new StyleSheet(public_path());
$buildPath = $this->config['css_build_path'];
$this->provider = new StyleSheet(public_path());
$this->buildPath = $this->config['css_build_path'];
$this->attributes = $attributes;

return $this->process($file, $provider, $buildPath);
$this->process($file);

return $this;
}

/**
* @param $file
* @param $provider
* @param $buildPath
* @return string
*/
private function process($file, $provider, $buildPath)
private function process($file)
{
$provider->add($file);
$this->provider->add($file);

if (in_array($this->environment, $this->config['ignore_envionments']))
if($this->provider->make($this->buildPath))
{
return $provider->tags();
$this->provider->minify();
}
}

//Return when minified file already exists
if(!$provider->make($buildPath))
/**
* @return mixed
*/
public function render()
{
if (in_array($this->environment, $this->config['ignore_environments']))
{
return $provider->tag($buildPath . $provider->getFilename());
return $this->provider->tags($this->attributes);
}

$provider->minify();
return $this->provider->tag($this->buildPath . $this->provider->getFilename(), $this->attributes);
}

/**
* @return string
*/
public function __toString()
{
return $this->render();
}

return $provider->tag($buildPath . $provider->getFilename());
/**
* @param array $config
* @throws Exceptions\InvalidArgumentException
* @return array
*/
private function checkConfiguration(array $config)
{
if(!isset($config['css_build_path']) || !is_string($config['css_build_path']))
throw new InvalidArgumentException("Missing css_build_path field");
if(!isset($config['js_build_path']) || !is_string($config['js_build_path']))
throw new InvalidArgumentException("Missing js_build_path field");
if(!isset($config['ignore_environments']) || !is_array($config['ignore_environments']))
throw new InvalidArgumentException("Missing ignore_environments field");
}
}
}
2 changes: 1 addition & 1 deletion src/CeesVanEgmond/Minify/MinifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function register()
array(
'css_build_path' => Config::get('minify::css_build_path'),
'js_build_path' => Config::get('minify::js_build_path'),
'ignore_envionments' => Config::get('minify::ignore_envionments'),
'ignore_environments' => Config::get('minify::ignore_environments'),
),
$app->environment()
);
Expand Down
38 changes: 36 additions & 2 deletions src/CeesVanEgmond/Minify/Providers/BaseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ public function add($file)
}

/**
* @param $attributes
* @return string
*/
public function tags()
public function tags($attributes)
{
$html = '';
foreach($this->files as $file)
{
$file = str_replace($this->publicPath, '', $file);
$html .= $this->tag($file);
$html .= $this->tag($file, $attributes);
}

return $html;
Expand Down Expand Up @@ -152,6 +153,39 @@ protected function buildMinifiedFilename()
$this->filename = $this->getHashedFilename() . $this->countModificationTime() . static::EXTENSION;
}

/**
* Build an HTML attribute string from an array.
*
* @param array $attributes
* @return string
*/
protected function attributes($attributes)
{
$html = array();
foreach ((array) $attributes as $key => $value)
{
$element = $this->attributeElement($key, $value);

if ( ! is_null($element)) $html[] = $element;
}

return count($html) > 0 ? ' '.implode(' ', $html) : '';
}

/**
* Build a single attribute element.
*
* @param string $key
* @param string $value
* @return string
*/
protected function attributeElement($key, $value)
{
if (is_numeric($key)) $key = $value;

if ( ! is_null($value)) return $key.'="'.htmlentities($value, ENT_QUOTES, 'UTF-8', false).'"';
}

/**
* @return string
*/
Expand Down
7 changes: 5 additions & 2 deletions src/CeesVanEgmond/Minify/Providers/JavaScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public function minify()

/**
* @param $file
* @param array $attributes
* @return string
*/
public function tag($file)
public function tag($file, array $attributes)
{
return "<script src='{$file}'></script>";
$attributes['src'] = $file;

return "<script{$this->attributes($attributes)}></script>" . PHP_EOL;
}
}
8 changes: 6 additions & 2 deletions src/CeesVanEgmond/Minify/Providers/StyleSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ public function minify()

/**
* @param $file
* @param array $attributes
* @return string
*/
public function tag($file)
public function tag($file, array $attributes = array())
{
return "<link href='{$file}' rel='stylesheet'>";
$attributes['href'] = $file;
$attributes['rel'] = 'stylesheet';

return "<link{$this->attributes($attributes)}>" . PHP_EOL;
}
}
4 changes: 2 additions & 2 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
|
*/

'ignore_envionments' => array(
'local',
'ignore_environments' => array(
'local',
),

/*
Expand Down

0 comments on commit fd72081

Please sign in to comment.