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

WatchCommand::collectSources()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 16
nc 6
nop 1
1
<?php
2
3
namespace JK\SamBundle\Command;
4
5
use Exception;
6
use JK\SamBundle\Watcher\Indexer\FileIndexer;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
12
class WatchCommand extends AbstractAssetsCommand
13
{
14
    /**
15
     * Indicate if the watching loop should continue.
16
     *
17
     * @var bool
18
     */
19
    protected $shouldStop = false;
20
21
    /**
22
     *
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('jk:assets:watch')
28
            ->setDescription('Watch the assets according to your tasks configuration ("jk_assets")')
29
        ;
30
    }
31
32
    /**
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     * @return int|null|void
36
     * @throws Exception
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $this->io = new SymfonyStyle($input, $output);
41
        $this
42
            ->io
43
            ->title('Symfony PHP Assets Manager');
44
        $this
45
            ->io
46
            ->section('Watch assets changes');
47
48
        $configuration = $this->loadConfiguration($input);
49
50
        // get debug mode
51
        $this->debug = $configuration['debug'];
52
53
        if ($this->debug) {
54
            $this->io->note('Debug Mode...');
55
        }
56
57
        // on Ctrl+C, we must stop to watch files
58
        pcntl_signal(SIGTERM, [$this, 'stopWatch']);
59
        pcntl_signal(SIGINT, [$this, 'stopWatch']);
60
61
        $indexer = new FileIndexer();
62
        $sources = $this->collectSources($configuration['tasks']);
63
64
        // start watching sources
65
        $this
66
            ->io
67
            ->text('Watching...');
68
69
        while (!$this->shouldStop) {
70
            $indexer->index($sources);
71
72
            if ($indexer->hasChangedEntries()) {
73
                $this
74
                    ->io
75
                    ->note('Sources has been modified...');
76
77
                $runCommand = new RunCommand();
78
                $runCommand->setContainer($this->container);
79
                $runCommand->run(new ArrayInput([]), $output);
80
81
                $this
82
                    ->io
83
                    ->text('Watching...');
84
            }
85
86
            pcntl_signal_dispatch();
87
            sleep(1);
88
        }
89
90
        // display end message
91
        $this->io->success('Assets watching end');
92
93
        return;
94
    }
95
96
    public function stopWatch()
97
    {
98
        $this->shouldStop = true;
99
        $this
100
            ->io
101
            ->note('Stop watching changes');
102
    }
103
104
    /**
105
     * @param array $tasks
106
     *
107
     * @return array
108
     */
109
    protected function collectSources(array $tasks)
110
    {
111
        $sources = [];
112
        $directories = [];
113
        $tasks = $this->buildTasks($tasks);
114
115
        foreach ($tasks as $task) {
116
            $sources = array_merge($sources, $task->getSources());
117
        }
118
        foreach ($sources as $source) {
119
120
            if (is_dir($source)) {
121
                $directory = $source;
122
            } else {
123
                $directory = dirname($source);
124
            }
125
            $this
126
                ->io
127
                ->text('Found new directory : '.$directory);
128
            $directories[] = $directory;
129
        }
130
131
        return $directories;
132
    }
133
}
134