Completed
Push — master ( 6685f6...03cc5c )
by Arnaud
11s
created

RunCommand::runManagedTask()   B

Complexity

Conditions 6
Paths 21

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 39
rs 8.439
cc 6
eloc 25
nc 21
nop 2
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 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
        return 0;
91
    }
92
93
    /**
94
     * @param TaskRunner $runner
95
     * @param Task $task
96
     * @throws Exception
97
     */
98
    protected function runManagedTask(TaskRunner $runner, Task $task)
99
    {
100
        $notificationSubscriber = $this
101
            ->container
102
            ->get('jk.assets.notification_subscriber');
103
104
        try {
105
            $this->io->text('- Running '.$task->getName());
106
            $runner->run($task);
107
108
            foreach ($notificationSubscriber->getNotifications() as $notification) {
109
                $this->io->text('  <info>x</info> '.$notification);
110
            }
111
            $notificationSubscriber->clearNotifications();
112
            $this->io->newLine();
113
114
        } catch (Exception $e) {
115
116
            if ($this->debug) {
117
                foreach ($notificationSubscriber->getNotifications() as $index => $notification) {
118
119
                    if ($index == count($notificationSubscriber->getNotifications())) {
120
                        $notification = '<error>x</error> '.$notification;
121
                    } else {
122
                        $notification = '<info>x</info>'.$notification;
123
                    }
124
                    $this->io->text($notification);
125
                }
126
                $notificationSubscriber->clearNotifications();
127
            }
128
129
            throw new Exception(
130
                'An error has been encountered during the execution of the task '.$task->getName()."\n"
131
                .$e->getMessage(),
132
                0,
133
                $e
134
            );
135
        }
136
    }
137
}
138