Skip to content

Commit 4b36d2a

Browse files
committed
Updated Laravel legacy files
1 parent f6ef2cf commit 4b36d2a

File tree

4 files changed

+129
-16
lines changed

4 files changed

+129
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ An ultra-simple-to-use assets management PHP library.
4242
<a id="frameworks"></a>
4343
## Supported frameworks
4444

45-
The library is framework agnostic and it should work well with any framework or naked PHP application. Nevertheless, since the library is most popular between Laravel users the following instructions have been tailored for **Laravel 5** framework ([still on Laravel 4?](https://github.com/Stolz/Assets/issues/55#issuecomment-73024822)). If you want to use the library in any other scenario please read the [non static interface](#nonstatic) instructions.
45+
The library is framework agnostic and it should work well with any framework or naked PHP application. Nevertheless, since the library is most popular between Laravel users the following instructions have been tailored for **Laravel 5** framework ([still on Laravel 4?](https://github.com/Stolz/Assets/issues/84#issuecomment-171149242)). If you want to use the library in any other scenario please read the [non static interface](#nonstatic) instructions.
4646

4747
<a id="installation"></a>
4848
## Installation

src/Laravel/FlushPipelineCommand.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace Stolz\Assets\Laravel;
22

3-
use Config;
4-
use File;
53
use Illuminate\Console\Command;
4+
use Illuminate\Contracts\Config\Repository as Config;
5+
use Illuminate\Filesystem\Filesystem;
66
use Symfony\Component\Console\Input\InputOption;
77

88
class FlushPipelineCommand extends Command
@@ -21,6 +21,28 @@ class FlushPipelineCommand extends Command
2121
*/
2222
protected $description = 'Flush assets pipeline files.';
2323

24+
/**
25+
* Class constructor.
26+
*
27+
* @param \Illuminate\Contracts\Config\Repository $config
28+
* @param \Illuminate\Filesystem\Filesystem $filesystem
29+
*
30+
* @return void
31+
*/
32+
public function __construct()//Config $config, Filesystem $filesystem (See NOTE below)
33+
{
34+
parent::__construct();
35+
36+
// NOTE: Dependency injection for Artisan commands constructor was not introduced until Laravel 5.1 (LST).
37+
// In order to keep compatibility with Laravel 5.0 we manually resolve the dependencies
38+
39+
//$this->config = $config;
40+
//$this->filesystem = $filesystem;
41+
42+
$this->config = app(Config::class);
43+
$this->filesystem = app(Filesystem::class);
44+
}
45+
2446
/**
2547
* Execute the console command.
2648
*
@@ -39,7 +61,7 @@ public function fire()
3961
foreach($directories as $dir)
4062
$this->comment($dir);
4163

42-
if( ! $this->confirm('Do you wish to continue? [yes|no]'))
64+
if( ! $this->confirm('Do you want to continue?'))
4365
return;
4466
}
4567

@@ -50,9 +72,9 @@ public function fire()
5072
$this->output->write("$dir ");
5173

5274
if($this->purgeDir($dir))
53-
$this->info('Ok');
75+
$this->info('OK');
5476
else
55-
$this->error('Error');
77+
$this->error('ERROR');
5678
}
5779

5880
$this->comment('Done!');
@@ -66,7 +88,7 @@ public function fire()
6688
protected function getPipelineDirectories()
6789
{
6890
// Parse configured groups
69-
$config = config('assets', []);
91+
$config = $this->config->get('assets', []);
7092
$groups = (isset($config['default'])) ? $config : ['default' => $config];
7193
if( ! is_null($group = $this->option('group')))
7294
$groups = array_only($groups, $group);
@@ -76,33 +98,40 @@ protected function getPipelineDirectories()
7698
foreach($groups as $group => $config)
7799
{
78100
$pipelineDir = (isset($config['pipeline_dir'])) ? $config['pipeline_dir'] : 'min';
101+
$publicDir = (isset($config['public_dir'])) ? $config['public_dir'] : public_path();
102+
$publicDir = rtrim($publicDir, DIRECTORY_SEPARATOR);
79103

80104
$cssDir = (isset($config['css_dir'])) ? $config['css_dir'] : 'css';
81-
$directories[] = public_path($cssDir . DIRECTORY_SEPARATOR . $pipelineDir);
82-
105+
$directories[] = implode(DIRECTORY_SEPARATOR, [$publicDir, $cssDir, $pipelineDir]);
83106

84107
$jsDir = (isset($config['js_dir'])) ? $config['js_dir'] : 'js';
85-
$directories[] = public_path($jsDir . DIRECTORY_SEPARATOR . $pipelineDir);
108+
$directories[] = implode(DIRECTORY_SEPARATOR, [$publicDir, $jsDir, $pipelineDir]);
86109
}
87110

88-
return array_unique($directories);
111+
// Clean results
112+
$directories = array_unique($directories);
113+
sort($directories);
114+
115+
return $directories;
89116
}
90117

91118
/**
92-
* Purge directory.
119+
* Remove the contents of a given directory.
93120
*
94121
* @param string $directory
122+
*
95123
* @return boolean
96124
*/
97125
protected function purgeDir($directory)
98126
{
99-
if( ! File::isDirectory($directory))
127+
if( ! $this->filesystem->isDirectory($directory))
100128
return true;
101129

102-
if(File::isWritable($directory))
103-
return File::cleanDirectory($directory);
130+
if($this->filesystem->isWritable($directory))
131+
return $this->filesystem->cleanDirectory($directory);
104132

105133
$this->error($directory . ' is not writable');
134+
106135
return false;
107136
}
108137

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php namespace Stolz\Assets\Laravel;
2+
3+
use Config;
4+
use File;
5+
use Illuminate\Console\Command;
6+
use Symfony\Component\Console\Input\InputOption;
7+
8+
class LegacyFlushPipelineCommand extends Command
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'asset:flush';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Flush assets pipeline files.';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return void
28+
*/
29+
public function fire()
30+
{
31+
// Get directory paths
32+
$pipeDir = Config::get('assets::pipeline_dir', 'min');
33+
$cssDir = public_path(Config::get('assets::css_dir', 'css') . DIRECTORY_SEPARATOR . $pipeDir);
34+
$jsDir = public_path(Config::get('assets::js_dir', 'js') . DIRECTORY_SEPARATOR . $pipeDir);
35+
36+
// Ask for confirmation
37+
if( ! $this->option('force'))
38+
{
39+
$this->info(sprintf('All content of %s and %s will be deleted.', $cssDir, $jsDir));
40+
if( ! $this->confirm('Do you wish to continue? [yes|no]'))
41+
return;
42+
}
43+
44+
// Purge assets
45+
$purgeCss = $this->purgeDir($cssDir);
46+
$purgeJs = $this->purgeDir($jsDir);
47+
48+
if( ! $purgeCss or ! $purgeJs)
49+
return $this->error('Something went wrong');
50+
51+
$this->info('Done!');
52+
}
53+
54+
/**
55+
* Purge directory.
56+
*
57+
* @param string $directory
58+
* @return boolean
59+
*/
60+
protected function purgeDir($directory)
61+
{
62+
if( ! File::isDirectory($directory))
63+
return true;
64+
65+
if(File::isWritable($directory))
66+
return File::cleanDirectory($directory);
67+
68+
$this->error($directory . ' is not writable');
69+
70+
return false;
71+
}
72+
73+
/**
74+
* Get the console command options.
75+
*
76+
* @return array
77+
*/
78+
protected function getOptions()
79+
{
80+
return array(
81+
array('force', 'f', InputOption::VALUE_NONE, 'Do not prompt for confirmation'),
82+
);
83+
}
84+
}

src/Laravel/LegacyServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function register()
4444

4545
// Bind 'stolz.assets.command.flush' component to the IoC container
4646
$this->app->bind('stolz.assets.command.flush', function ($app) {
47-
return new FlushPipelineCommand();
47+
return new LegacyFlushPipelineCommand();
4848
});
4949
}
5050
}

0 commit comments

Comments
 (0)