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

Commit fd72081

Browse files
committed
Merge branch 'develop'
2 parents 6469fe9 + 8719245 commit fd72081

File tree

10 files changed

+145
-35
lines changed

10 files changed

+145
-35
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ You can use this Facade anywhere in your application
4343
{{ Minify::stylesheet('/css/main.css') }}
4444
//or by passing multiple files
4545
{{ Minify::stylesheet(array('/css/main.css', '/css/bootstrap.css')) }}
46+
//add custom attributes
47+
{{ Minify::stylesheet(array('/css/main.css', '/css/bootstrap.css'), array('foo' => 'bar')) }}
4648
</head>
4749
...
4850
</html>
@@ -60,6 +62,8 @@ You can use this Facade anywhere in your application
6062
{{ Minify::javascript('/js/jquery.js') }}
6163
//or by passing multiple files
6264
{{ Minify::javascript(array('/js/jquery.js', '/js/jquery-ui.js')) }}
65+
//add custom attributes
66+
{{ Minify::javascript(array('/js/jquery.js', '/js/jquery-ui.js'), array('bar' => 'baz')) }}
6367
</html>
6468

6569
```
@@ -79,7 +83,7 @@ return array(
7983
|
8084
*/
8185

82-
'ignore_envionments' => array(
86+
'ignore_environments' => array(
8387
'local',
8488
),
8589

@@ -119,14 +123,14 @@ return array(
119123
```php
120124
<?php
121125
$config = array(
122-
'ignore_envionments' => 'local',
126+
'ignore_environments' => 'local',
123127
'js_build_path' => '/js/builds/',
124128
'css_builds_path' => '/css/builds',
125129
)
126130
$minify = new CeesVanEgmond\Minify\Providers\Javascript($public_path);
127131
$minify->add($file)
128132

129-
if (in_array($environment, $config['ignore_envionments']))
133+
if (in_array($environment, $config['ignore_environments']))
130134
{
131135
return $provider->tags();
132136
}

spec/CeesVanEgmond/Minify/Providers/StyleSheetSpec.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ function it_adds_multiple_files()
3636
$this->shouldHaveCount(2);
3737
}
3838

39+
function it_adds_custom_attributes()
40+
{
41+
$this->tag('file', array('foobar' => 'baz'))
42+
->shouldReturn('<link foobar="baz" href="file" rel="stylesheet">' . PHP_EOL);
43+
}
44+
45+
function it_adds_without_custom_attributes()
46+
{
47+
$this->tag('file')
48+
->shouldReturn('<link href="file" rel="stylesheet">' . PHP_EOL);
49+
}
50+
3951
function it_throws_exception_when_file_not_exists()
4052
{
4153
$this->shouldThrow('CeesVanEgmond\Minify\Exceptions\FileNotExistException')

src/CeesVanEgmond/Minify/Contracts/MinifyInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public function minify();
99

1010
/**
1111
* @param $file
12+
* @param $attributes
1213
* @return mixed
1314
*/
14-
public function tag($file);
15+
public function tag($file, array $attributes);
1516
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php namespace CeesVanEgmond\Minify\Exceptions;
2+
3+
class InvalidArgumentException extends \Exception{}
Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace CeesVanEgmond\Minify;
22

3+
use CeesVanEgmond\Minify\Exceptions\InvalidArgumentException;
34
use CeesVanEgmond\Minify\Providers\JavaScript;
45
use CeesVanEgmond\Minify\Providers\StyleSheet;
56

@@ -10,68 +11,116 @@ class Minify
1011
*/
1112
protected $config;
1213

14+
/**
15+
* @var array
16+
*/
17+
protected $attributes = array();
18+
1319
/**
1420
* @var string
1521
*/
1622
private $environment;
1723

24+
/**
25+
* @var
26+
*/
27+
private $provider;
28+
29+
/**
30+
* @var
31+
*/
32+
private $buildPath;
33+
1834
/**
1935
* @param array $config
20-
* @param $environment
36+
* @param string $environment
2137
*/
2238
public function __construct(array $config, $environment)
2339
{
40+
$this->checkConfiguration($config);
41+
2442
$this->config = $config;
2543
$this->environment = $environment;
2644
}
2745

2846
/**
2947
* @param $file
48+
* @param array $attributes
3049
* @return string
3150
*/
32-
public function javascript($file)
51+
public function javascript($file, $attributes = array())
3352
{
34-
$provider = new JavaScript(public_path());
35-
$buildPath = $this->config['js_build_path'];
53+
$this->provider = new JavaScript(public_path());
54+
$this->buildPath = $this->config['js_build_path'];
55+
$this->attributes = $attributes;
56+
57+
$this->process($file);
3658

37-
return $this->process($file, $provider, $buildPath);
59+
return $this;
3860
}
3961

4062
/**
4163
* @param $file
64+
* @param array $attributes
4265
* @return string
4366
*/
44-
public function stylesheet($file)
67+
public function stylesheet($file, $attributes = array())
4568
{
46-
$provider = new StyleSheet(public_path());
47-
$buildPath = $this->config['css_build_path'];
69+
$this->provider = new StyleSheet(public_path());
70+
$this->buildPath = $this->config['css_build_path'];
71+
$this->attributes = $attributes;
4872

49-
return $this->process($file, $provider, $buildPath);
73+
$this->process($file);
74+
75+
return $this;
5076
}
5177

5278
/**
5379
* @param $file
54-
* @param $provider
55-
* @param $buildPath
56-
* @return string
5780
*/
58-
private function process($file, $provider, $buildPath)
81+
private function process($file)
5982
{
60-
$provider->add($file);
83+
$this->provider->add($file);
6184

62-
if (in_array($this->environment, $this->config['ignore_envionments']))
85+
if($this->provider->make($this->buildPath))
6386
{
64-
return $provider->tags();
87+
$this->provider->minify();
6588
}
89+
}
6690

67-
//Return when minified file already exists
68-
if(!$provider->make($buildPath))
91+
/**
92+
* @return mixed
93+
*/
94+
public function render()
95+
{
96+
if (in_array($this->environment, $this->config['ignore_environments']))
6997
{
70-
return $provider->tag($buildPath . $provider->getFilename());
98+
return $this->provider->tags($this->attributes);
7199
}
72100

73-
$provider->minify();
101+
return $this->provider->tag($this->buildPath . $this->provider->getFilename(), $this->attributes);
102+
}
103+
104+
/**
105+
* @return string
106+
*/
107+
public function __toString()
108+
{
109+
return $this->render();
110+
}
74111

75-
return $provider->tag($buildPath . $provider->getFilename());
112+
/**
113+
* @param array $config
114+
* @throws Exceptions\InvalidArgumentException
115+
* @return array
116+
*/
117+
private function checkConfiguration(array $config)
118+
{
119+
if(!isset($config['css_build_path']) || !is_string($config['css_build_path']))
120+
throw new InvalidArgumentException("Missing css_build_path field");
121+
if(!isset($config['js_build_path']) || !is_string($config['js_build_path']))
122+
throw new InvalidArgumentException("Missing js_build_path field");
123+
if(!isset($config['ignore_environments']) || !is_array($config['ignore_environments']))
124+
throw new InvalidArgumentException("Missing ignore_environments field");
76125
}
77-
}
126+
}

src/CeesVanEgmond/Minify/MinifyServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function register()
4141
array(
4242
'css_build_path' => Config::get('minify::css_build_path'),
4343
'js_build_path' => Config::get('minify::js_build_path'),
44-
'ignore_envionments' => Config::get('minify::ignore_envionments'),
44+
'ignore_environments' => Config::get('minify::ignore_environments'),
4545
),
4646
$app->environment()
4747
);

src/CeesVanEgmond/Minify/Providers/BaseProvider.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,16 @@ public function add($file)
8585
}
8686

8787
/**
88+
* @param $attributes
8889
* @return string
8990
*/
90-
public function tags()
91+
public function tags($attributes)
9192
{
9293
$html = '';
9394
foreach($this->files as $file)
9495
{
9596
$file = str_replace($this->publicPath, '', $file);
96-
$html .= $this->tag($file);
97+
$html .= $this->tag($file, $attributes);
9798
}
9899

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

156+
/**
157+
* Build an HTML attribute string from an array.
158+
*
159+
* @param array $attributes
160+
* @return string
161+
*/
162+
protected function attributes($attributes)
163+
{
164+
$html = array();
165+
foreach ((array) $attributes as $key => $value)
166+
{
167+
$element = $this->attributeElement($key, $value);
168+
169+
if ( ! is_null($element)) $html[] = $element;
170+
}
171+
172+
return count($html) > 0 ? ' '.implode(' ', $html) : '';
173+
}
174+
175+
/**
176+
* Build a single attribute element.
177+
*
178+
* @param string $key
179+
* @param string $value
180+
* @return string
181+
*/
182+
protected function attributeElement($key, $value)
183+
{
184+
if (is_numeric($key)) $key = $value;
185+
186+
if ( ! is_null($value)) return $key.'="'.htmlentities($value, ENT_QUOTES, 'UTF-8', false).'"';
187+
}
188+
155189
/**
156190
* @return string
157191
*/

src/CeesVanEgmond/Minify/Providers/JavaScript.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ public function minify()
2222

2323
/**
2424
* @param $file
25+
* @param array $attributes
2526
* @return string
2627
*/
27-
public function tag($file)
28+
public function tag($file, array $attributes)
2829
{
29-
return "<script src='{$file}'></script>";
30+
$attributes['src'] = $file;
31+
32+
return "<script{$this->attributes($attributes)}></script>" . PHP_EOL;
3033
}
3134
}

src/CeesVanEgmond/Minify/Providers/StyleSheet.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ public function minify()
2222

2323
/**
2424
* @param $file
25+
* @param array $attributes
2526
* @return string
2627
*/
27-
public function tag($file)
28+
public function tag($file, array $attributes = array())
2829
{
29-
return "<link href='{$file}' rel='stylesheet'>";
30+
$attributes['href'] = $file;
31+
$attributes['rel'] = 'stylesheet';
32+
33+
return "<link{$this->attributes($attributes)}>" . PHP_EOL;
3034
}
3135
}

src/config/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
|
1313
*/
1414

15-
'ignore_envionments' => array(
16-
'local',
15+
'ignore_environments' => array(
16+
'local',
1717
),
1818

1919
/*

0 commit comments

Comments
 (0)