Skip to content

Commit 20adda9

Browse files
author
Paul M. Jones
authored
Merge pull request #25 from afilina/generator
Add generator script.
2 parents 82d143d + 75771ab commit 20adda9

File tree

9 files changed

+236
-18
lines changed

9 files changed

+236
-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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ copyright terms of the package contents.
4141
Quickly validate your project's compliance by following these steps:
4242

4343
- Install package in your project: `composer require pds/skeleton @dev`
44-
- Run the validator: `./vendor/pds/skeleton/bin/validate`
44+
- Run the validator: `vendor/bin/pdsskeleton validate`
45+
46+
## Generator
47+
48+
Generate a compliant package skeleton by following these steps:
49+
50+
- Install package in your project: `composer require pds/skeleton @dev`
51+
- Run the generator: `vendor/bin/pdsskeleton generate`
4552

4653
## Root-Level Directories
4754

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/" }
9+
},
10+
"bin": ["bin/pdsskeleton"]
711
}

bin/validate renamed to src/ComplianceValidator.php

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/Console.php

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

src/PackageGenerator.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Pds\Skeleton;
4+
5+
use Pds\Skeleton\ComplianceValidator;
6+
7+
class PackageGenerator
8+
{
9+
public function execute()
10+
{
11+
$validator = new ComplianceValidator();
12+
$lines = $validator->getFiles();
13+
$validatorResults = $validator->validate($lines);
14+
$files = $this->createFiles($validatorResults);
15+
$this->outputResults($files);
16+
return true;
17+
}
18+
19+
public function createFiles($validatorResults, $root = null)
20+
{
21+
if ($root == null) {
22+
$root = realpath(__DIR__ . "/../../../../");
23+
}
24+
$files = $this->createFileList($validatorResults);
25+
$createdFiles = [];
26+
foreach ($files as $i => $file) {
27+
$isDir = substr($file, -1, 1) == '/';
28+
if ($isDir) {
29+
$path = $root . '/' . substr($file, 0, -1);
30+
$createdFiles[$file] = $path;
31+
mkdir($path, 0755);
32+
continue;
33+
}
34+
$path = $root . '/' . $file . '.md';
35+
$createdFiles[$file] = $file . '.md';
36+
file_put_contents($path, '');
37+
chmod($path, 0644);
38+
}
39+
return $createdFiles;
40+
}
41+
42+
public function createFileList($validatorResults)
43+
{
44+
$files = [];
45+
foreach ($validatorResults as $label => $complianceResult) {
46+
if (in_array($complianceResult['state'], [
47+
ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
48+
ComplianceValidator::STATE_REQUIRED_NOT_PRESENT,
49+
])) {
50+
$files[$label] = $complianceResult['expected'];
51+
}
52+
}
53+
return $files;
54+
}
55+
56+
public function outputResults($results)
57+
{
58+
foreach ($results as $file) {
59+
echo "Created {$file}" . PHP_EOL;
60+
}
61+
}
62+
}

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.

tests/PackageGeneratorTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
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;
16+
use Pds\Skeleton\PackageGenerator;
17+
18+
$tester = new PackageGeneratorTest();
19+
$tester->testGenerate_WithMissingBin_ReturnsBin();
20+
21+
echo "Errors: {$tester->numErrors}" . PHP_EOL;
22+
23+
class PackageGeneratorTest
24+
{
25+
public $numErrors = 0;
26+
27+
public function testGenerate_WithMissingBin_ReturnsBin()
28+
{
29+
$validatorResults = [
30+
'bin/' => [
31+
'state' => ComplianceValidator::STATE_OPTIONAL_NOT_PRESENT,
32+
'expected' => 'bin/',
33+
],
34+
'config/' => [
35+
'state' => ComplianceValidator::STATE_INCORRECT_PRESENT,
36+
'expected' => 'config/',
37+
],
38+
];
39+
40+
$generator = new PackageGenerator();
41+
$files = $generator->createFileList($validatorResults);
42+
43+
if (!array_key_exists('bin/', $files)) {
44+
$this->numErrors++;
45+
echo __FUNCTION__ . ": Expected bin/ to be present" . PHP_EOL;
46+
}
47+
48+
if (array_key_exists('config/', $files)) {
49+
$this->numErrors++;
50+
echo __FUNCTION__ . ": Expected config/ to be absent" . PHP_EOL;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)