Skip to content

Commit

Permalink
Refactored validator to support more cli commands. Added bin to paren…
Browse files Browse the repository at this point in the history
…t project.
  • Loading branch information
afilina committed Jan 2, 2017
1 parent 6605a8a commit b0c7422
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ copyright terms of the package contents.
Quickly validate your project's compliance by following these steps:

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

## Root-Level Directories

Expand Down
20 changes: 20 additions & 0 deletions bin/pdsskeleton
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env php

<?php

$autoloadFiles = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
];

foreach ($autoloadFiles as $autoloadFile) {
if (file_exists($autoloadFile)) {
require_once $autoloadFile;
break;
}
}

use PDS\Skeleton\Console;

$console = new Console();
$console->execute($argv);
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"type": "standard",
"description": "Standard for PHP package skeletons.",
"homepage": "https://github.com/php-pds/skeleton",
"license": "CC-BY-SA-4.0"
"license": "CC-BY-SA-4.0",
"autoload": {
"psr-4": { "PDS\\Skeleton\\": "src/PDS/Skeleton" }
},
"bin": ["bin/pdsskeleton"]
}
44 changes: 30 additions & 14 deletions bin/validate → src/PDS/Skeleton/ComplianceValidator.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
#!/usr/bin/env php

<?php

if (!defined('ENV') || ENV != 'test') {
$lines = scandir(__DIR__ . "/../../../../");
foreach ($lines as $i => $line) {
if (is_dir($line)) {
$lines[$i] .= "/";
}
}
$validator = new ComplianceValidator();
$results = $validator->validate($lines);
$validator->outputResults($results);
}
namespace PDS\Skeleton;

class ComplianceValidator
{
Expand All @@ -21,6 +9,16 @@ class ComplianceValidator
const STATE_REQUIRED_NOT_PRESENT = 3;
const STATE_INCORRECT_PRESENT = 4;

protected $files = null;

public function execute()
{
$lines = $this->getFiles();
$results = $this->validate($lines);
$this->outputResults($results);
return true;
}

public function validate($lines)
{
$complianceTests = [
Expand Down Expand Up @@ -53,6 +51,24 @@ public function validate($lines)
return $results;
}

/**
* Get list of files and directories previously set, or generate from parent project.
*/
public function getFiles()
{
if ($this->files == null) {
$files = scandir(__DIR__ . "/../../../../");
foreach ($files as $i => $file) {
if (is_dir($file)) {
$files[$i] .= "/";
}
}
$this->files = $files;
}

return $this->files;
}

public function outputResults($results)
{
foreach ($results as $result) {
Expand Down Expand Up @@ -288,4 +304,4 @@ protected function checkResources($lines)
'Ressources/',
]);
}
}
}
43 changes: 43 additions & 0 deletions src/PDS/Skeleton/Console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace PDS\Skeleton;

class Console
{
protected $commandsWhitelist = [
'validate' => 'PDS\Skeleton\ComplianceValidator',
];

public function execute($args)
{
if (count($args) > 1) {

$executable = array_shift($args);
$commandName = array_shift($args);

if (array_key_exists($commandName, $this->commandsWhitelist)) {
return $this->executeCommand($this->commandsWhitelist[$commandName], $args);
}

$this->outputHelp();
return false;
}

$this->outputHelp();
return false;
}

protected function executeCommand($commandClass, $args)
{
$command = new $commandClass();
return $command->execute($args);
}

protected function outputHelp()
{
echo 'Available commands:' . PHP_EOL;
foreach ($this->commandsWhitelist as $key => $value) {
echo 'pdsskeleton ' . $key . PHP_EOL;
}
}
}
15 changes: 13 additions & 2 deletions tests/ComplianceValidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<?php

define('ENV', 'test');
require __DIR__ . "/../bin/validate";
$autoloadFiles = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
];

foreach ($autoloadFiles as $autoloadFile) {
if (file_exists($autoloadFile)) {
require_once $autoloadFile;
break;
}
}

use PDS\Skeleton\ComplianceValidator;

$tester = new ComplianceValidatorTest();
// Test all 4 possible states.
Expand Down

0 comments on commit b0c7422

Please sign in to comment.