1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace JK\SamBundle\Command; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use JK\Sam\File\Locator; |
7
|
|
|
use JK\Sam\File\Normalizer; |
8
|
|
|
use JK\Sam\Task\Task;; |
9
|
|
|
use JK\Sam\Task\TaskRunner; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
15
|
|
|
|
16
|
|
|
class RunCommand extends AbstractAssetsCommand implements ContainerAwareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Configure the task name. |
20
|
|
|
*/ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this |
24
|
|
|
->setName('jk:assets:build') |
25
|
|
|
->setDescription('Build the assets according to your assets configuration ("jk_assets")') |
26
|
|
|
->addOption('config', 'c', InputOption::VALUE_OPTIONAL, |
27
|
|
|
'If defined, this file will be used to load the assets configuration. It should be an yml file ' |
28
|
|
|
.'containing an array of tasks. |
29
|
|
|
jk_assets: |
30
|
|
|
____tasks: |
31
|
|
|
________// your configuration |
32
|
|
|
________... |
33
|
|
|
' |
34
|
|
|
) |
35
|
|
|
; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param InputInterface $input |
40
|
|
|
* @param OutputInterface $output |
41
|
|
|
* |
42
|
|
|
* @return int|null|void |
43
|
|
|
* |
44
|
|
|
* @throws Exception |
45
|
|
|
*/ |
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
|
|
$this->io = new SymfonyStyle($input, $output); |
49
|
|
|
$this |
50
|
|
|
->io |
51
|
|
|
->title('Symfony PHP Assets Manager'); |
52
|
|
|
|
53
|
|
|
// get debug mode |
54
|
|
|
$this->debug = $this |
55
|
|
|
->container |
56
|
|
|
->getParameter('jk.assets.debug'); |
57
|
|
|
|
58
|
|
|
if ($this->debug) { |
59
|
|
|
$this->io->note('Debug Mode...'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// build tasks to run |
63
|
|
|
$tasks = $this->buildTasks($input); |
64
|
|
|
|
65
|
|
|
// build required filters |
66
|
|
|
$filters = $this->buildFilters(); |
67
|
|
|
|
68
|
|
|
// run task with configured filter |
69
|
|
|
$normalizer = new Normalizer($this->container->getParameter('kernel.root_dir').'/../'); |
70
|
|
|
$runner = new TaskRunner($filters, new Locator($normalizer), $this->debug); |
71
|
|
|
$this->io->text('- Running tasks...'); |
72
|
|
|
|
73
|
|
|
// run tasks |
74
|
|
|
foreach ($tasks as $index => $task) { |
75
|
|
|
$this->runManagedTask($runner, $task); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// display end message |
79
|
|
|
$this->io->success('Assets build end'); |
80
|
|
|
|
81
|
|
|
return 0; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param TaskRunner $runner |
86
|
|
|
* @param Task $task |
87
|
|
|
* @throws Exception |
88
|
|
|
*/ |
89
|
|
|
protected function runManagedTask(TaskRunner $runner, Task $task) |
90
|
|
|
{ |
91
|
|
|
$notificationSubscriber = $this |
92
|
|
|
->container |
93
|
|
|
->get('jk.assets.notification_subscriber'); |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$this->io->text('- Running '.$task->getName()); |
97
|
|
|
$runner->run($task); |
98
|
|
|
|
99
|
|
|
foreach ($notificationSubscriber->getNotifications() as $notification) { |
100
|
|
|
$this->io->text(' <info>x</info> '.$notification); |
101
|
|
|
} |
102
|
|
|
$notificationSubscriber->clearNotifications(); |
103
|
|
|
$this->io->newLine(); |
104
|
|
|
|
105
|
|
|
} catch (Exception $e) { |
106
|
|
|
|
107
|
|
|
if ($this->debug) { |
108
|
|
|
foreach ($notificationSubscriber->getNotifications() as $index => $notification) { |
109
|
|
|
|
110
|
|
|
if ($index == count($notificationSubscriber->getNotifications())) { |
111
|
|
|
$notification = '<error>x</error> '.$notification; |
112
|
|
|
} else { |
113
|
|
|
$notification = '<info>x</info>'.$notification; |
114
|
|
|
} |
115
|
|
|
$this->io->text($notification); |
116
|
|
|
} |
117
|
|
|
$notificationSubscriber->clearNotifications(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
throw new Exception( |
121
|
|
|
'An error has been encountered during the execution of the task '.$task->getName()."\n" |
122
|
|
|
.$e->getMessage(), |
123
|
|
|
0, |
124
|
|
|
$e |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.