Skip to content

Commit 2ae8b3e

Browse files
first commit
0 parents  commit 2ae8b3e

File tree

8 files changed

+207
-0
lines changed

8 files changed

+207
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vendor/
2+
.idea/
3+
composer.lock
4+
phpunit.xml
5+
.phpunit.result.cache

Applier.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace PatrickMaynard\AuditClassGenerator;
4+
5+
class Applier
6+
{
7+
public function getDirContents($dir){
8+
$results = array();
9+
$files = scandir($dir);
10+
11+
foreach($files as $key => $value){
12+
if(!is_dir($dir. DIRECTORY_SEPARATOR .$value)){
13+
$results[] = $dir. DIRECTORY_SEPARATOR . $value;
14+
} else if(
15+
is_dir($dir. DIRECTORY_SEPARATOR .$value) &&
16+
!str_contains($value, '..') &&
17+
$value !== '.'
18+
) {
19+
$results[] = $value;
20+
$results = array_merge($results, $this->getDirContents($dir. DIRECTORY_SEPARATOR .$value));
21+
}
22+
}
23+
return $results;
24+
25+
}
26+
27+
//TODO: Allow blade as well.
28+
public function filterAllFilesForTwigExtension()
29+
{
30+
$directoryToParse = getcwd() . '..' . DIRECTORY_SEPARATOR . ' .. ';
31+
echo PHP_EOL . 'We will be looking recursively in ' . $directoryToParse . ' for Twig files .. ' . PHP_EOL;
32+
$files = $this->getDirContents($directoryToParse);
33+
$output = [];
34+
foreach ($files as $file) {
35+
if (str_ends_with($file, '.html.twig')) {
36+
$output[] = $file;
37+
}
38+
}
39+
return $output;
40+
}
41+
42+
43+
function applyAllAuditTags()
44+
{
45+
//First, we need to be on a safe git branch, since this set of changes will mess with lots of files
46+
exec('git checkout -b temporary-template-audit-branch-remove-me-soon');
47+
48+
echo PHP_EOL;
49+
echo "Temporary git branch created. Iterating through templates ... ";
50+
echo PHP_EOL;
51+
52+
$files = $this->filterAllFilesForTwigExtension();
53+
54+
foreach ($files as $index => $file) {
55+
$contents = file_get_contents($file);
56+
preg_match_all('/class="[^"]+"/', $contents, $matches);
57+
foreach ($matches as $index => $match) {
58+
if ( $index = 0 ) {
59+
continue;
60+
}
61+
62+
if (is_array($match)) {
63+
foreach ($match as $matchingString) {
64+
$hash = md5(rand(0,1000000000) . "This will provide a nice, long, randomish string.");
65+
$shorterFingerprint = 'audit_' . substr($hash, 0, 6);
66+
$withSpace = ' ' . $shorterFingerprint . ' ';
67+
68+
//Now just do a simple string replacement
69+
$contents = str_replace(
70+
$matchingString, substr($matchingString, 0, -1) . $withSpace . '"', $contents
71+
);
72+
}
73+
}
74+
75+
}
76+
file_put_contents($file, $contents);
77+
}
78+
echo "Done!";
79+
echo PHP_EOL;
80+
}
81+
}

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Audit class generator
2+
================
3+
4+
This simple package add pseudorandomized audit tags to your Twig html class definitions.
5+
6+
This can be useful if you don't have traditional debugging tools available.
7+
8+
By seeing a short, pseudorandom audit tag among the classes applied to an html element,
9+
you can quickly determine exactly which Twig template the html element came from.
10+
11+
That can save significant debugging time, since you don't have to guess at which Twig
12+
template to modify for a given task.
13+
14+
## Installation
15+
16+
To install this tool as a dev dependency:
17+
18+
```
19+
composer require --dev patrick-maynard/audit-class-generator
20+
```
21+
22+
Installing this in a production environment is not recommended.
23+
24+
## Usage
25+
26+
One the tool has been installed, you can use the following command to open a new
27+
git branch and apply the temporary audit classes to any Twig html elements that
28+
already have a "class" attribute defined.
29+
30+
```
31+
vendor/patrick-maynard/audit-class-generator/apply.php
32+
```
33+
34+
Happy debugging!

TODOS.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Todo items
2+
===
3+
4+
Here are some todo items for this project:
5+
6+
* x Push this to a new GitHub repository
7+
* Do any tagging necessary to get this onto packagist
8+
* Make a clone of the Tag Monk project and remove the .git folder
9+
* Do a git init to set up the test project as its own safe repository locally
10+
* Do a composer require for the auditing library
11+
* Test out the process of running the applier and viewing browser output
12+
* Iterate as necessary to get everything working as it should be
13+
* Publicize this via the Twig stack trace dumper readme and vice-versa

Tests/InstantiationTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace PatrickMaynard\AuditClassGenerator\Tests;
4+
5+
use PatrickMaynard\AuditClassGenerator\Applier;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class InstantiationTest extends TestCase
9+
{
10+
public function testInstantiation(): void
11+
{
12+
$applier = new Applier;
13+
14+
//If there were no excpetions, we're good for now.
15+
self::assertTrue(true);
16+
}
17+
}

apply.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace PatrickMaynard\AuditClassGenerator;
4+
5+
$applier = new Applier();
6+
7+
$applier->applyAllAuditTags();

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "patrick-maynard/audit-class-generator",
3+
"type": "library",
4+
"description": "A simple tool that allows developers to see which Twig templates are being used to generate a page",
5+
"keywords": ["twig", "html", "debugging"],
6+
"homepage": "http://home.patrickmaynard.com",
7+
"license": "MIT",
8+
"minimum-stability": "dev",
9+
"authors": [
10+
{
11+
"name": "Patrick Maynard",
12+
"email": "patrick.maynard@markenfilm-space.de",
13+
"homepage": "http://home.patrickmaynard.com",
14+
"role": "Developer"
15+
}
16+
],
17+
"require": {
18+
"php": ">=7.4",
19+
"twig/twig": "*"
20+
},
21+
"require-dev": {
22+
"symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0"
23+
},
24+
"autoload": {
25+
"psr-4" : { "PatrickMaynard\\AuditClassGenerator\\" : "" },
26+
"exclude-from-classmap": [
27+
"/Tests/"
28+
]
29+
}
30+
}

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" failOnRisky="true" failOnWarning="true">
3+
<coverage>
4+
<include>
5+
<directory>./</directory>
6+
</include>
7+
<exclude>
8+
<directory>./Tests</directory>
9+
<directory>./vendor</directory>
10+
</exclude>
11+
</coverage>
12+
<php>
13+
<ini name="error_reporting" value="-1"/>
14+
</php>
15+
<testsuites>
16+
<testsuite name="Twig Stack Trace Dumper Extension Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
</phpunit>

0 commit comments

Comments
 (0)