|
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 BuildCommand extends AbstractCommand 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 and filters. |
|
29
|
|
|
jk.assets: |
|
30
|
|
|
____tasks: |
|
31
|
|
|
________// your configuration |
|
32
|
|
|
________... |
|
33
|
|
|
____filters: ... |
|
34
|
|
|
' |
|
35
|
|
|
) |
|
36
|
|
|
; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param InputInterface $input |
|
41
|
|
|
* @param OutputInterface $output |
|
42
|
|
|
* |
|
43
|
|
|
* @return int|null|void |
|
44
|
|
|
* |
|
45
|
|
|
* @throws Exception |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->io = new SymfonyStyle($input, $output); |
|
50
|
|
|
$this |
|
51
|
|
|
->io |
|
52
|
|
|
->title('Symfony PHP Assets Manager'); |
|
53
|
|
|
|
|
54
|
|
|
// load configuration from container or given file path |
|
55
|
|
|
$configuration = $this->loadConfiguration($input); |
|
56
|
|
|
|
|
57
|
|
|
// get debug mode |
|
58
|
|
|
$this->debug = $configuration['debug']; |
|
59
|
|
|
|
|
60
|
|
|
if ($this->debug) { |
|
61
|
|
|
$this->io->note('Debug Mode...'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// build tasks to run |
|
65
|
|
|
$tasks = $this->buildTasks($configuration['tasks']); |
|
66
|
|
|
|
|
67
|
|
|
// build required filters |
|
68
|
|
|
$filters = $this->buildFilters($configuration['filters']); |
|
69
|
|
|
|
|
70
|
|
|
// run task with configured filter |
|
71
|
|
|
$normalizer = new Normalizer($this->container->getParameter('kernel.root_dir').'/../'); |
|
72
|
|
|
$locator = new Locator($normalizer); |
|
73
|
|
|
|
|
74
|
|
|
// create the runner |
|
75
|
|
|
$runner = new TaskRunner( |
|
76
|
|
|
$filters, |
|
77
|
|
|
$locator, |
|
78
|
|
|
$this->debug |
|
79
|
|
|
); |
|
80
|
|
|
$this->io->text('- Running tasks...'); |
|
81
|
|
|
|
|
82
|
|
|
// run tasks |
|
83
|
|
|
foreach ($tasks as $task) { |
|
84
|
|
|
$this->runManagedTask($runner, $task); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// display end message |
|
88
|
|
|
$this->io->success('Assets build end'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param TaskRunner $runner |
|
93
|
|
|
* @param Task $task |
|
94
|
|
|
* @throws Exception |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function runManagedTask(TaskRunner $runner, Task $task) |
|
97
|
|
|
{ |
|
98
|
|
|
$notificationSubscriber = $this |
|
99
|
|
|
->container |
|
100
|
|
|
->get('jk.assets.notification_subscriber'); |
|
101
|
|
|
|
|
102
|
|
|
try { |
|
103
|
|
|
$this->io->text('- Running '.$task->getName()); |
|
104
|
|
|
$runner->run($task); |
|
105
|
|
|
|
|
106
|
|
|
foreach ($notificationSubscriber->getNotifications() as $notification) { |
|
107
|
|
|
$this->io->text(' <info>x</info> '.$notification); |
|
108
|
|
|
} |
|
109
|
|
|
$notificationSubscriber->clearNotifications(); |
|
110
|
|
|
$this->io->newLine(); |
|
111
|
|
|
|
|
112
|
|
|
} catch (Exception $e) { |
|
113
|
|
|
|
|
114
|
|
|
if ($this->debug) { |
|
115
|
|
|
foreach ($notificationSubscriber->getNotifications() as $index => $notification) { |
|
116
|
|
|
|
|
117
|
|
|
if ($index == count($notificationSubscriber->getNotifications())) { |
|
118
|
|
|
$notification = '<error>x</error> '.$notification; |
|
119
|
|
|
} else { |
|
120
|
|
|
$notification = '<info>x</info>'.$notification; |
|
121
|
|
|
} |
|
122
|
|
|
$this->io->text($notification); |
|
123
|
|
|
} |
|
124
|
|
|
$notificationSubscriber->clearNotifications(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
throw new Exception( |
|
128
|
|
|
'An error has been encountered during the execution of the task '.$task->getName()."\n" |
|
129
|
|
|
.$e->getMessage(), |
|
130
|
|
|
0, |
|
131
|
|
|
$e |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|