Skip to content

Commit b0c7422

Browse files
committed
Refactored validator to support more cli commands. Added bin to parent project.
1 parent 6605a8a commit b0c7422

File tree

7 files changed

+113
-18
lines changed

7 files changed

+113
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ copyright terms of the package contents.
4343
Quickly validate your project's compliance by following these steps:
4444

4545
- Install package in your project: `composer require pds/skeleton @dev`
46-
- Run the validator: `./vendor/pds/skeleton/bin/validate`
46+
- Run the validator: `vendor/bin/pdsskeleton validate`
4747

4848
## Root-Level Directories
4949

bin/pdsskeleton

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
5+
$autoloadFiles = [
6+
__DIR__ . '/../vendor/autoload.php',
7+
__DIR__ . '/../../../autoload.php'
8+
];
9+
10+
foreach ($autoloadFiles as $autoloadFile) {
11+
if (file_exists($autoloadFile)) {
12+
require_once $autoloadFile;
13+
break;
14+
}
15+
}
16+
17+
use PDS\Skeleton\Console;
18+
19+
$console = new Console();
20+
$console->execute($argv);

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
"type": "standard",
44
"description": "Standard for PHP package skeletons.",
55
"homepage": "https://github.com/php-pds/skeleton",
6-
"license": "CC-BY-SA-4.0"
6+
"license": "CC-BY-SA-4.0",
7+
"autoload": {
8+
"psr-4": { "PDS\\Skeleton\\": "src/PDS/Skeleton" }
9+
},
10+
"bin": ["bin/pdsskeleton"]
711
}
Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
#!/usr/bin/env php
2-
31
<?php
42

5-
if (!defined('ENV') || ENV != 'test') {
6-
$lines = scandir(__DIR__ . "/../../../../");
7-
foreach ($lines as $i => $line) {
8-
if (is_dir($line)) {
9-
$lines[$i] .= "/";
10-
}
11-
}
12-
$validator = new ComplianceValidator();
13-
$results = $validator->validate($lines);
14-
$validator->outputResults($results);
15-
}
3+
namespace PDS\Skeleton;
164

175
class ComplianceValidator
186
{
@@ -21,6 +9,16 @@ class ComplianceValidator
219
const STATE_REQUIRED_NOT_PRESENT = 3;
2210
const STATE_INCORRECT_PRESENT = 4;
2311

12+
protected $files = null;
13+
14+
public function execute()
15+
{
16+
$lines = $this->getFiles();
17+
$results = $this->validate($lines);
18+
$this->outputResults($results);
19+
return true;
20+
}
21+
2422
public function validate($lines)
2523
{
2624
$complianceTests = [
@@ -53,6 +51,24 @@ public function validate($lines)
5351
return $results;
5452
}
5553

54+
/**
55+
* Get list of files and directories previously set, or generate from parent project.
56+
*/
57+
public function getFiles()
58+
{
59+
if ($this->files == null) {
60+
$files = scandir(__DIR__ . "/../../../../");
61+
foreach ($files as $i => $file) {
62+
if (is_dir($file)) {
63+
$files[$i] .= "/";
64+
}
65+
}
66+
$this->files = $files;
67+
}
68+
69+
return $this->files;
70+
}
71+
5672
public function outputResults($results)
5773
{
5874
foreach ($results as $result) {
@@ -288,4 +304,4 @@ protected function checkResources($lines)
288304
'Ressources/',
289305
]);
290306
}
291-
}
307+
}

src/PDS/Skeleton/Console.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace PDS\Skeleton;
4+
5+
class Console
6+
{
7+
protected $commandsWhitelist = [
8+
'validate' => 'PDS\Skeleton\ComplianceValidator',
9+
];
10+
11+
public function execute($args)
12+
{
13+
if (count($args) > 1) {
14+
15+
$executable = array_shift($args);
16+
$commandName = array_shift($args);
17+
18+
if (array_key_exists($commandName, $this->commandsWhitelist)) {
19+
return $this->executeCommand($this->commandsWhitelist[$commandName], $args);
20+
}
21+
22+
$this->outputHelp();
23+
return false;
24+
}
25+
26+
$this->outputHelp();
27+
return false;
28+
}
29+
30+
protected function executeCommand($commandClass, $args)
31+
{
32+
$command = new $commandClass();
33+
return $command->execute($args);
34+
}
35+
36+
protected function outputHelp()
37+
{
38+
echo 'Available commands:' . PHP_EOL;
39+
foreach ($this->commandsWhitelist as $key => $value) {
40+
echo 'pdsskeleton ' . $key . PHP_EOL;
41+
}
42+
}
43+
}

tests/ComplianceValidatorTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
<?php
22

3-
define('ENV', 'test');
4-
require __DIR__ . "/../bin/validate";
3+
$autoloadFiles = [
4+
__DIR__ . '/../vendor/autoload.php',
5+
__DIR__ . '/../../../autoload.php'
6+
];
7+
8+
foreach ($autoloadFiles as $autoloadFile) {
9+
if (file_exists($autoloadFile)) {
10+
require_once $autoloadFile;
11+
break;
12+
}
13+
}
14+
15+
use PDS\Skeleton\ComplianceValidator;
516

617
$tester = new ComplianceValidatorTest();
718
// Test all 4 possible states.

0 commit comments

Comments
 (0)