-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6283 from kenjis/feat-spark-filter-check
feat: `spark filter:check` command
- Loading branch information
Showing
6 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Commands\Utilities; | ||
|
||
use CodeIgniter\CLI\BaseCommand; | ||
use CodeIgniter\CLI\CLI; | ||
use CodeIgniter\Commands\Utilities\Routes\FilterCollector; | ||
use Config\Services; | ||
|
||
/** | ||
* Check filters for a route. | ||
*/ | ||
class FilterCheck extends BaseCommand | ||
{ | ||
/** | ||
* The group the command is lumped under | ||
* when listing commands. | ||
* | ||
* @var string | ||
*/ | ||
protected $group = 'CodeIgniter'; | ||
|
||
/** | ||
* The Command's name | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'filter:check'; | ||
|
||
/** | ||
* the Command's short description | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Check filters for a route.'; | ||
|
||
/** | ||
* the Command's usage | ||
* | ||
* @var string | ||
*/ | ||
protected $usage = 'filter:check <HTTP method> <route>'; | ||
|
||
/** | ||
* the Command's Arguments | ||
* | ||
* @var array<string, string> | ||
*/ | ||
protected $arguments = [ | ||
'method' => 'The HTTP method. get, post, put, etc.', | ||
'route' => 'The route (URI path) to check filtes.', | ||
]; | ||
|
||
/** | ||
* the Command's Options | ||
* | ||
* @var array | ||
*/ | ||
protected $options = []; | ||
|
||
/** | ||
* @return int exit code | ||
*/ | ||
public function run(array $params) | ||
{ | ||
if (! isset($params[0], $params[1])) { | ||
CLI::error('You must specify a HTTP verb and a route.'); | ||
CLI::write(' Usage: ' . $this->usage); | ||
CLI::write('Example: filter:check get /'); | ||
CLI::write(' filter:check put products/1'); | ||
|
||
return EXIT_ERROR; | ||
} | ||
|
||
$method = strtolower($params[0]); | ||
$route = $params[1]; | ||
|
||
// Load Routes | ||
$routes = Services::routes(); | ||
require APPPATH . 'Config/Routes.php'; | ||
$routes->getRoutes('*'); // Triggers discovery | ||
|
||
$filterCollector = new FilterCollector(); | ||
|
||
$filters = $filterCollector->get($method, $route); | ||
|
||
// PageNotFoundException | ||
if ($filters['before'] === ['<unknown>']) { | ||
CLI::error( | ||
"Can't find a route: " . | ||
CLI::color( | ||
'"' . strtoupper($method) . ' ' . $route . '"', | ||
'black', | ||
'light_gray' | ||
), | ||
); | ||
|
||
return EXIT_ERROR; | ||
} | ||
|
||
$tbody[] = [ | ||
strtoupper($method), | ||
$route, | ||
implode(' ', $filters['before']), | ||
implode(' ', $filters['after']), | ||
]; | ||
|
||
$thead = [ | ||
'Method', | ||
'Route', | ||
'Before Filters', | ||
'After Filters', | ||
]; | ||
|
||
CLI::table($tbody, $thead); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <admin@codeigniter.com> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Commands; | ||
|
||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\StreamFilterTrait; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class FilterCheckTest extends CIUnitTestCase | ||
{ | ||
use StreamFilterTrait; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->resetServices(); | ||
parent::setUp(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->resetServices(); | ||
parent::tearDown(); | ||
} | ||
|
||
protected function getBuffer() | ||
{ | ||
return $this->getStreamFilterBuffer(); | ||
} | ||
|
||
public function testFilterCheckDefinedRoute() | ||
{ | ||
command('filter:check get /'); | ||
|
||
$this->assertStringContainsString( | ||
'|GET|/||toolbar|', | ||
str_replace(' ', '', $this->getBuffer()) | ||
); | ||
} | ||
|
||
public function testFilterCheckInvalidRoute() | ||
{ | ||
command('filter:check put product/123'); | ||
|
||
$this->assertStringContainsString( | ||
'Can\'t find a route: "PUT product/123"', | ||
str_replace( | ||
["\033[0m", "\033[1;31m", "\033[0;30m", "\033[47m"], | ||
'', | ||
$this->getBuffer() | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters