Completed
Pull Request — master (#1)
by Arnaud
03:14 queued 01:18
created

RunCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 8.

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.

Loading history...
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