-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathFindCommandHandler.php
More file actions
198 lines (168 loc) · 5.56 KB
/
FindCommandHandler.php
File metadata and controls
198 lines (168 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/*
* This file is part of the puli/cli package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Puli\Cli\Handler;
use Puli\Cli\Util\StringUtil;
use Puli\Discovery\Api\Discovery;
use Puli\Repository\Discovery\ResourceBinding;
use Puli\Repository\Api\ResourceRepository;
use RuntimeException;
use Webmozart\Console\Api\Args\Args;
use Webmozart\Console\Api\IO\IO;
use Webmozart\Console\UI\Component\Table;
use Webmozart\Console\UI\Style\TableStyle;
use Webmozart\Expression\Expr;
/**
* Handles the "find" command.
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FindCommandHandler
{
/**
* @var ResourceRepository
*/
private $repo;
/**
* @var Discovery
*/
private $discovery;
/**
* Creates the handler.
*
* @param ResourceRepository $repo The resource repository.
* @param Discovery $discovery The discovery.
*/
public function __construct(ResourceRepository $repo, Discovery $discovery)
{
$this->repo = $repo;
$this->discovery = $discovery;
}
/**
* Handles the "find" command.
*
* @param Args $args The console arguments.
* @param IO $io The I/O.
*
* @return int The status code.
*/
public function handle(Args $args, IO $io)
{
$criteria = array();
if ($args->isOptionSet('path')) {
$criteria['path'] = $args->getOption('path');
$criteria['language'] = $args->getOption('language');
}
if ($args->isOptionSet('name')) {
if (isset($criteria['path'])) {
throw new RuntimeException('The options --name and --path cannot be combined.');
}
$criteria['path'] = '/**/'.$args->getOption('name');
$criteria['language'] = $args->getOption('language');
}
if ($args->isOptionSet('class')) {
$criteria['class'] = $args->getOption('class');
}
if ($args->isOptionSet('type')) {
$criteria['bindingType'] = $args->getOption('type');
}
if (empty($criteria)) {
throw new RuntimeException('No search criteria specified.');
}
return $this->listMatches($io, $criteria);
}
/**
* Lists the matches for the given search criteria.
*
* @param IO $io The I/O.
* @param array $criteria The array with the optional keys "pattern",
* "shortClass" and "bindingType".
*
* @return int The status code.
*/
private function listMatches(IO $io, array $criteria)
{
if (isset($criteria['path']) && isset($criteria['bindingType'])) {
$matches = array_intersect_key(
$this->findByPath($criteria['path'], $criteria['language']),
$this->findByBindingType($criteria['bindingType'])
);
} elseif (isset($criteria['path'])) {
$matches = $this->findByPath($criteria['path'], $criteria['language']);
} elseif (isset($criteria['bindingType'])) {
$matches = $this->findByBindingType($criteria['bindingType']);
} else {
$matches = $this->findByPath('/*');
}
if (isset($criteria['class'])) {
$shortClass = $criteria['class'];
$matches = array_filter($matches, function ($value) use ($shortClass) {
return $value === $shortClass;
});
}
$this->printTable($io, $matches);
return 0;
}
/**
* Finds the resources for a given path pattern.
*
* @param string $query The resource query.
* @param string $language The language of the query.
*
* @return string[] An array of short resource class names indexed by
* the resource path.
*/
private function findByPath($query, $language = 'glob')
{
$matches = array();
$query = '/'.ltrim($query, '/');
foreach ($this->repo->find($query, $language) as $resource) {
$matches[$resource->getPath()] = StringUtil::getShortClassName(get_class($resource));
}
return $matches;
}
/**
* Finds the resources for a given binding type.
*
* @param string $typeName The type name.
*
* @return string[] An array of short resource class names indexed by
* the resource path.
*/
private function findByBindingType($typeName)
{
$matches = array();
$expr = Expr::isInstanceOf('Puli\Repository\Discovery\ResourceBinding');
foreach ($this->discovery->findBindings($typeName, $expr) as $binding) {
/** @var ResourceBinding $binding */
foreach ($binding->getResources() as $resource) {
$matches[$resource->getPath()] = StringUtil::getShortClassName(get_class($resource));
}
}
ksort($matches);
return $matches;
}
/**
* Prints the given resources.
*
* @param IO $io The I/O.
* @param string[] $matches An array of short resource class names indexed
* by the resource path.
*/
private function printTable(IO $io, array $matches)
{
$table = new Table(TableStyle::borderless());
foreach ($matches as $path => $shortClass) {
$table->addRow(array($shortClass, sprintf('<c1>%s</c1>', $path)));
}
$table->render($io);
}
}