Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ jobs:
# 4️⃣ PSR-12 Code Style Check
- name: Check PSR-12 Coding Standards
run: |
composer global require "squizlabs/php_codesniffer=*"
phpcs --standard=PSR12 --extensions=php -n . bin/audit-class-generator
composer global require squizlabs/php_codesniffer
phpcs --standard=PSR12 --extensions=php -n \
bin/audit-class-generator \
src \
tests

# 5️⃣ Run PHPUnit Tests
- name: Run PHPUnit Tests
run: vendor/bin/phpunit --colors=always Tests/
run: vendor/bin/phpunit --colors=always tests/
65 changes: 18 additions & 47 deletions bin/audit-class-generator
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#!/usr/bin/env php
<?php declare(strict_types=1);
<?php

use PatrickMaynard\AuditClassGenerator\Applier;
use PatrickMaynard\AuditClassGenerator\Config;
declare(strict_types=1);

use PatrickMaynard\AuditClassGenerator\Application\BusinessLogic\Applier;
use PatrickMaynard\AuditClassGenerator\Application\Config\Config;
use PatrickMaynard\AuditClassGenerator\Infrastructure\FileSystem\DirectoryScanner;
use PatrickMaynard\AuditClassGenerator\Infrastructure\FileSystem\ExtensionFilter;
use PatrickMaynard\AuditClassGenerator\Infrastructure\FileSystem\FileIO;

include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';

Expand Down Expand Up @@ -34,53 +39,19 @@ echo PHP_EOL;

$applier = new Applier();

$files = filterAllFilesForTwigExtension($config);
$scanner = new DirectoryScanner();
$filter = new ExtensionFilter($config->getExtension());
$fileIO = new FileIO();

foreach ($files as $index => $file) {
$contents = file_get_contents($file);
foreach ($scanner->scan($config->getDirectory()) as $file) {
if (!$filter->matches($file)) {
continue;
}

file_put_contents($file, $applier->applyAllAuditTags($contents));
$content = $fileIO->read($file);
$processed = $applier->applyAllAuditTags($content);
$fileIO->write($file, $processed);
}

echo "Done!";
echo PHP_EOL;

//TODO: Allow blade as well. Maybe solved with configuration
function filterAllFilesForTwigExtension(Config $config): array
{
$directoryToParse = getcwd() . DIRECTORY_SEPARATOR . $config->getDirectory();

echo PHP_EOL . 'We will be looking recursively in ' . $directoryToParse . ' for Twig files .. ' . PHP_EOL;

$files = getDirContents($directoryToParse);
$output = [];

foreach ($files as $file) {
if (str_ends_with($file, $config->getExtension())) {
$output[] = $file;
}
}

return $output;
}

function getDirContents($dir): array
{
$results = [];
$files = scandir($dir);

foreach ($files as $key => $value) {
if (!is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
$results[] = $dir . DIRECTORY_SEPARATOR . $value;
} elseif (
is_dir($dir . DIRECTORY_SEPARATOR . $value)
&& !str_contains($value, '..')
&& $value !== '.'
) {
$results[] = $value;
$results = array_merge($results, getDirContents($dir . DIRECTORY_SEPARATOR . $value));
}
}

return $results;
}
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
"twig/twig": "*"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0"
"phpunit/phpunit": "^9.6",
"mikey179/vfsstream": "^1.6"
},
"autoload": {
"psr-4" : { "PatrickMaynard\\AuditClassGenerator\\" : "" },
"exclude-from-classmap": [
"/Tests/"
]
"psr-4" : {
"PatrickMaynard\\AuditClassGenerator\\" : "src/"
}
},
"autoload-dev": {
"psr-4" : {
"PatrickMaynard\\AuditClassGenerator\\Tests\\" : "tests/"
}
}
}
2 changes: 1 addition & 1 deletion Applier.php → src/Application/BusinessLogic/Applier.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace PatrickMaynard\AuditClassGenerator;
namespace PatrickMaynard\AuditClassGenerator\Application\BusinessLogic;

class Applier
{
Expand Down
10 changes: 6 additions & 4 deletions Config.php → src/Application/Config/Config.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types = 1);

namespace PatrickMaynard\AuditClassGenerator;
declare(strict_types=1);

class Config {
namespace PatrickMaynard\AuditClassGenerator\Application\Config;

class Config
{
private string $directory;
private string $extension;
private bool $verbose;
Expand All @@ -22,7 +24,7 @@ public static function create(array $options): self
{
return new self(
$options['directory'] ?? $options['d'] ?? 'templates',
$options['extension'] ?? $options['e'] ?? '.html.twig',
$options['extension'] ?? $options['e'] ?? 'html.twig',
isset($options['v']) || isset($options['verbose'])
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Infrastructure/Exception/EmptyFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PatrickMaynard\AuditClassGenerator\Infrastructure\Exception;

class EmptyFileException extends \RuntimeException
{
public function __construct(string $path)
{
parent::__construct(
sprintf('File is empty: %s', $path)
);
}
}
15 changes: 15 additions & 0 deletions src/Infrastructure/Exception/FileDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PatrickMaynard\AuditClassGenerator\Infrastructure\Exception;

class FileDoesNotExistException extends \RuntimeException
{
public function __construct(string $path)
{
parent::__construct(
sprintf('File does not exist: %s', $path)
);
}
}
15 changes: 15 additions & 0 deletions src/Infrastructure/Exception/FileNotWritableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace PatrickMaynard\AuditClassGenerator\Infrastructure\Exception;

class FileNotWritableException extends \RuntimeException
{
public function __construct(string $path)
{
parent::__construct(
sprintf('File not writable: %s', $path)
);
}
}
24 changes: 24 additions & 0 deletions src/Infrastructure/FileSystem/DirectoryScanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace PatrickMaynard\AuditClassGenerator\Infrastructure\FileSystem;

class DirectoryScanner
{
public function scan(string $path): iterable
{
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
$path,
\FilesystemIterator::SKIP_DOTS
)
);

foreach ($iterator as $file) {
if ($file->isFile()) {
yield $file->getPathname();
}
}
}
}
17 changes: 17 additions & 0 deletions src/Infrastructure/FileSystem/ExtensionFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PatrickMaynard\AuditClassGenerator\Infrastructure\FileSystem;

class ExtensionFilter
{
public function __construct(private string $extension)
{
}

public function matches(string $filePath): bool
{
return pathinfo($filePath, PATHINFO_EXTENSION) === $this->extension;
}
}
42 changes: 42 additions & 0 deletions src/Infrastructure/FileSystem/FileIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace PatrickMaynard\AuditClassGenerator\Infrastructure\FileSystem;

use PatrickMaynard\AuditClassGenerator\Infrastructure\Exception\EmptyFileException;
use PatrickMaynard\AuditClassGenerator\Infrastructure\Exception\FileDoesNotExistException;
use PatrickMaynard\AuditClassGenerator\Infrastructure\Exception\FileNotWritableException;

class FileIO
{
public function read(string $path): string
{
if (!file_exists($path)) {
throw new FileDoesNotExistException("File does not exist: {$path}");
}

$contents = file_get_contents($path);

$hasBom = str_starts_with($contents, "\xEF\xBB\xBF");

if ($contents === '' || ($hasBom && strlen($contents) === 3)) {
throw new EmptyFileException($path);
}

return $contents;
}

public function write(string $path, string $content): void
{
if (!file_exists($path)) {
throw new FileDoesNotExistException($path);
}

if (!is_writable($path)) {
throw new FileNotWritableException($path);
}

file_put_contents($path, $content);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace PatrickMaynard\AuditClassGenerator\Tests;
namespace PatrickMaynard\AuditClassGenerator\Tests\Application\BusinessLogic;

use PatrickMaynard\AuditClassGenerator\Applier;
use PatrickMaynard\AuditClassGenerator\Application\BusinessLogic\Applier;
use PHPUnit\Framework\TestCase;

class ApplierTest extends TestCase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace PatrickMaynard\AuditClassGenerator\Tests;
namespace PatrickMaynard\AuditClassGenerator\Tests\Application\Config;

use PatrickMaynard\AuditClassGenerator\Applier;
use PatrickMaynard\AuditClassGenerator\Application\BusinessLogic\Applier;
use PHPUnit\Framework\TestCase;

class InstantiationTest extends TestCase
{
public function testInstantiation(): void
{
$applier = new Applier;
$applier = new Applier();

//If there were no excpetions, we're good for now.
self::assertTrue(true);
Expand Down
Loading