1 | <?php |
||
16 | class SearchCommand extends Command |
||
17 | { |
||
18 | /** |
||
19 | * @var Client |
||
20 | */ |
||
21 | private $packagist; |
||
22 | |||
23 | /** |
||
24 | * @param Client $packagist |
||
25 | */ |
||
26 | 1 | public function __construct(Client $packagist) |
|
27 | { |
||
28 | 1 | parent::__construct(); |
|
29 | |||
30 | 1 | $this->packagist = $packagist; |
|
31 | 1 | } |
|
32 | |||
33 | /** |
||
34 | * @return Client |
||
35 | */ |
||
36 | 1 | public function getPackagist() |
|
37 | { |
||
38 | 1 | return $this->packagist; |
|
39 | } |
||
40 | |||
41 | 1 | protected function configure() |
|
42 | { |
||
43 | 1 | $this |
|
44 | 1 | ->setName('search') |
|
45 | 1 | ->setDescription('Search available composer init templates') |
|
46 | 1 | ->addArgument( |
|
47 | 1 | 'filter', |
|
48 | 1 | InputArgument::OPTIONAL, |
|
49 | 'Filter by name' |
||
50 | 1 | ); |
|
51 | ; |
||
52 | 1 | } |
|
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | 1 | public function getTemplates() |
|
58 | { |
||
59 | 1 | $response = $this->packagist->get( |
|
60 | 1 | '/packages/list.json', |
|
61 | 1 | ['query' => ['type' => 'composer-init-template']] |
|
62 | 1 | ); |
|
63 | |||
64 | 1 | $list = json_decode($response->getBody(), true); |
|
65 | |||
66 | 1 | return (array) $list['packageNames']; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param array $array |
||
71 | * @param string $filter |
||
72 | * @return array |
||
73 | */ |
||
74 | public function filterWith(array $array, $filter) |
||
75 | { |
||
76 | 1 | return array_filter($array, function ($name) use ($filter) { |
|
77 | 1 | return false !== strpos($name, $filter); |
|
78 | 1 | }); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param InputInterface $input |
||
83 | * @param OutputInterface $output |
||
84 | */ |
||
85 | 1 | protected function execute(InputInterface $input, OutputInterface $output) |
|
105 | } |
||
106 |