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
28 changes: 14 additions & 14 deletions src/Application/BusinessLogic/Applier.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ public function applyAllAuditTags(string $contents): string
{
preg_match_all('/class="[^"]*"/', $contents, $matches);

foreach ($matches as $index => $match) {
if ($index = 0) {
foreach ($matches as $match) {
if (!is_array($match)) {
continue;
}

if (is_array($match)) {
foreach ($match as $matchingString) {
$hash = md5(rand(0, 1000000000) . "This will provide a nice, long, randomish string.");
$shorterFingerprint = 'audit_' . substr($hash, 0, 6);
$withSpace = ' ' . $shorterFingerprint . ' ';

//Now just do a simple string replacement
$contents = str_replace(
$matchingString,
substr($matchingString, 0, -1) . $withSpace . '"',
$contents,
);
foreach ($match as $matchingString) {
if (str_contains($matchingString, 'audit_')) {
continue;
}

$withSpace = AuditClassNameGenerator::generate();

//Now just do a simple string replacement
$contents = str_replace(
$matchingString,
substr($matchingString, 0, -1) . $withSpace . '"',
$contents,
);
}
}

Expand Down
26 changes: 26 additions & 0 deletions src/Application/BusinessLogic/AuditClassNameGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace PatrickMaynard\AuditClassGenerator\Application\BusinessLogic;

class AuditClassNameGenerator
{
protected const AUDIT_CLASS_PREFIX = 'audit';
protected const RANDOM_STRING_SUFFIX = 'This will provide a nice, long, randomish string.';
protected const MAX_HASH_LENGTH = 6;
protected const UPPER_THRESHOLD_RANDOM_INT = 1000000000;

public static function generate(): string
{
return sprintf(
' %s_%s ',
self::AUDIT_CLASS_PREFIX,
substr(
md5(random_int(0, self::UPPER_THRESHOLD_RANDOM_INT) . self::RANDOM_STRING_SUFFIX),
0,
self::MAX_HASH_LENGTH
)
);
}
}
12 changes: 12 additions & 0 deletions tests/Application/BusinessLogic/ApplierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace PatrickMaynard\AuditClassGenerator\Tests\Application\BusinessLogic;

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

class ApplierTest extends TestCase
{
protected const AUDIT_CLASS_PREFIX = 'audit_';

public function testSingleClassInjection(): void
{
$applier = new Applier();
Expand All @@ -16,6 +19,15 @@ public function testSingleClassInjection(): void

$this->assertMatchesRegularExpression('/class="foo audit_[0-9a-f]{6} "/', $output);
}
public function testReRunSingleClassInjection(): void
{
$applier = new Applier();

$input = '<div class="foo' . AuditClassNameGenerator::generate() . '"></div>';
$output = $applier->applyAllAuditTags($input);

$this->assertMatchesRegularExpression('/class="foo audit_[0-9a-f]{6} "/', $output);
}

public function testMultipleClasses(): void
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

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