1 | <?php |
||
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() |
||
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() |
||
103 | |||
104 | /** |
||
105 | * @param array $tasks |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | protected function collectSources(array $tasks) |
||
133 | } |
||
134 |