1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JK\SamBundle\Command; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use JK\Sam\Filter\FilterBuilder; |
7
|
|
|
use JK\Sam\Filter\FilterInterface; |
8
|
|
|
use JK\Sam\Task\Task; |
9
|
|
|
use JK\Sam\Task\TaskBuilder; |
10
|
|
|
use JK\SamBundle\Configuration\Loader\ConfigurationLoader; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
17
|
|
|
|
18
|
|
|
abstract class AbstractAssetsCommand extends Command implements ContainerAwareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var SymfonyStyle |
22
|
|
|
*/ |
23
|
|
|
protected $io; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var boolean |
27
|
|
|
*/ |
28
|
|
|
protected $debug = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ContainerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $container; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Build tasks from the configuration array. |
37
|
|
|
* |
38
|
|
|
* @param array $configuration |
39
|
|
|
* |
40
|
|
|
* @return Task[] |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
protected function buildTasks(array $configuration) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->io->text('- Building tasks...'); |
45
|
|
|
$builder = new TaskBuilder($this->debug); |
46
|
|
|
|
47
|
|
|
$tasks = $builder->build($configuration); |
48
|
|
|
|
49
|
|
|
$this->io->text('- Tasks build !'); |
50
|
|
|
$this->io->newLine(); |
51
|
|
|
|
52
|
|
|
return $tasks; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Build the filter according to the configuration array. |
57
|
|
|
* |
58
|
|
|
* @param array $configuration |
59
|
|
|
* |
60
|
|
|
* @return FilterInterface[] |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
protected function buildFilters(array $configuration) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->io->text('- Building filters...'); |
65
|
|
|
$builder = new FilterBuilder($this->container->get('event_dispatcher')); |
66
|
|
|
|
67
|
|
|
$filters = $builder->build($configuration); |
68
|
|
|
|
69
|
|
|
$this->io->text('- Filters build !'); |
70
|
|
|
$this->io->newLine(); |
71
|
|
|
|
72
|
|
|
return $filters; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Load the configuration from a yml file. |
77
|
|
|
* |
78
|
|
|
* @param $configurationFile |
79
|
|
|
* |
80
|
|
|
* @return string[] |
81
|
|
|
* |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
|
|
protected function loadConfigurationFile($configurationFile) |
85
|
|
|
{ |
86
|
|
|
if (!file_exists($configurationFile)) { |
87
|
|
|
throw new Exception('The configuration yml file '.$configurationFile.' was not found'); |
88
|
|
|
} |
89
|
|
|
$configuration = Yaml::parse(file_get_contents($configurationFile)); |
90
|
|
|
|
91
|
|
|
if (empty($configuration['jk_assets']['tasks'])) { |
92
|
|
|
throw new Exception('Tasks not found in configuration file '.$configurationFile); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $configuration['jk_assets']['tasks']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Load the configuration from a yml file or the container, according to the given option. |
100
|
|
|
* |
101
|
|
|
* @param InputInterface $input |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected function loadConfiguration(InputInterface $input) |
105
|
|
|
{ |
106
|
|
|
$loader = new ConfigurationLoader(); |
107
|
|
|
|
108
|
|
|
if ($input->hasOption('config') && $file = $input->getOption('config')) { |
109
|
|
|
$configuration = $loader->loadFromFile($file); |
110
|
|
|
} else { |
111
|
|
|
$configuration = $loader->loadFromContainer($this->container); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $configuration; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sets the container. |
119
|
|
|
* |
120
|
|
|
* @param ContainerInterface|null $container A ContainerInterface instance or null |
121
|
|
|
*/ |
122
|
|
|
public function setContainer(ContainerInterface $container = null) |
123
|
|
|
{ |
124
|
|
|
$this->container = $container; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.