1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: oguzu |
5
|
|
|
* Date: 27-3-2017 |
6
|
|
|
* Time: 16:52 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pbxg33k\MusicInfo\Command; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Pbxg33k\MusicInfo\MusicInfo; |
13
|
|
|
use Pbxg33k\Traits\HydratableTrait; |
14
|
|
|
use Pbxg33k\Traits\PropertyTrait; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Helper\Table; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
use Symfony\Component\Debug\Debug; |
22
|
|
|
use Symfony\Component\Yaml\Yaml; |
23
|
|
|
|
24
|
|
|
abstract class BaseCommand extends Command |
25
|
|
|
{ |
26
|
|
|
const COMMAND_PREFIX = 'music-info'; |
27
|
|
|
|
28
|
|
|
const COMMAND_NAME = 'undefined'; |
29
|
|
|
|
30
|
|
|
const COMMAND_DESCRIPTION = 'Description not set'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var MusicInfo |
34
|
|
|
*/ |
35
|
|
|
protected $musicInfo; |
36
|
|
|
|
37
|
2 |
|
protected function initializeMusicInfo() |
38
|
|
|
{ |
39
|
2 |
|
$config = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/config.yml')); |
40
|
|
|
|
41
|
2 |
|
$this->musicInfo = new MusicInfo($config['music_info']); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
2 |
|
protected function configure() |
45
|
|
|
{ |
46
|
2 |
|
if(!$this->musicInfo) { |
47
|
2 |
|
$this->initializeMusicInfo(); |
48
|
2 |
|
} |
49
|
|
|
|
50
|
2 |
|
$this |
51
|
2 |
|
->setName(static::COMMAND_PREFIX . ':' . static::COMMAND_NAME) |
52
|
2 |
|
->setDescription(static::COMMAND_DESCRIPTION) |
53
|
2 |
|
->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enables debug mode'); |
54
|
|
|
|
55
|
2 |
|
parent::configure(); |
56
|
2 |
|
} |
57
|
|
|
|
58
|
2 |
|
protected function initialize(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
2 |
|
if($input->getOption('debug')) { |
61
|
|
|
Debug::enable(); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
parent::initialize($input, $output); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $collection |
69
|
|
|
* @param $columns |
70
|
|
|
* @param Table $table |
71
|
|
|
* @return Table |
72
|
|
|
*/ |
73
|
2 |
|
protected function generateTableForSearchResult($collection, $columns, Table $table) |
74
|
|
|
{ |
75
|
2 |
|
$table->setHeaders(array_values($columns)); |
76
|
|
|
|
77
|
2 |
|
foreach($collection as $service => $serviceResult) { |
78
|
2 |
|
$table = $this->generateTableRows($serviceResult, $columns, $table); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
return $table; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $collection |
86
|
|
|
* @param $columns |
87
|
|
|
* @param Table $table |
88
|
|
|
* @return Table |
89
|
|
|
*/ |
90
|
2 |
|
protected function generateTableRows($collection, $columns, Table $table) |
91
|
|
|
{ |
92
|
2 |
|
foreach($collection as $item) { |
93
|
2 |
|
$row = []; |
94
|
|
|
|
95
|
2 |
|
foreach($columns as $columnKey => $columnValue) { |
96
|
2 |
|
$row[] = $item->getPropertyValue($columnKey); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
$table->addRow($row); |
100
|
2 |
|
} |
101
|
|
|
|
102
|
2 |
|
return $table; |
103
|
|
|
} |
104
|
|
|
} |