Skip to content

Commit

Permalink
Add initial set of files
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Mar 7, 2024
0 parents commit d816d14
Show file tree
Hide file tree
Showing 5 changed files with 2,960 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.castor.stub.php
/twig-include-syntaxe*
/vendor/
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Twig Include Syntax

Bored to update twig include syntax by hand? Me too!

You don't understand what I'm talking about? Let me explain.

When you include a file in twig, you have to use the following syntax:

```twig
{{ include('path/to/file.html.twig') }}
```

It's semantically more correct, than the following syntax:

```twig
{% include 'path/to/file.html.twig' %}
```

So I made this to apply this kind of changes:

```diff
{% block header %}
- {% include "_header.html.twig" %}
+ {{ include('_header.html.twig') }}
{% endblock %}
```

## Installation

Go to the release page and download the latest version.

You don't even need PHP installed on your machine, just download the static
binary file and you are ready to go.

## Warning

* This does't hand whitespace control
* It works on my projects, but it may not work on yours
* There is no tests and the regex is quite complex
68 changes: 68 additions & 0 deletions castor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

use Castor\Attribute\AsTask;
use Symfony\Component\Finder\Finder;

use function Castor\io;
use function Castor\run;

#[AsTask(description: 'Replace Twig include syntaxe')]
function replace(string $path): void
{
$files = Finder::create()
->in($path)
->files()
;

foreach ($files as $file) {
$content = file_get_contents($file);
if (!str_contains($content, 'include')) {
continue;
}

$callback = function (array $matches) {
$layout = '{{ include(\'%s\'%s%s) }}';

$templateName = $matches['templateName'];
$templateName = str_replace(':', '/', $templateName);
$templateName = ltrim($templateName, '/');

$variables = $only = '';

if (isset($matches['variables'])) {
$variables = ', {'.$matches['variables'].'}';
}

if (isset($matches['only'])) {
$only = ', with_context = false';
}

return sprintf($layout, $templateName, $variables, $only);
};

$pattern = '#{%[\s\\n]+include(?:\()?[\s\\n\(]+[\'"\(](?<templateName>[\w+\.\-_/@]+)[\'"](?:\))?(?:[\s\\n]+with[\s\\n]+{(?<variables>(?:.|\n)*)})?(?:[\s\\n]+(?<only>only)[\s\\n]+)?[\s\\n]*%}#U';

$content = preg_replace_callback($pattern, $callback, $content);

file_put_contents($file, $content);
}
}

#[AsTask()]
function package(): void
{
if (Phar::running(false)) {
throw new RuntimeException('This task must be run outside a phar');
}

io()->title('Packaging application');

io()->section('Installing vendor');
run(['composer', 'install', '--no-dev', '--optimize-autoloader']);

io()->section('Compiling phar');
run(['castor', 'repack', '--app-name', 'twig-include-syntaxe']);

io()->section('Compiling static binary');
run(['castor', 'compile', 'twig-include-syntaxe.linux.phar']);
}
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"require": {
"symfony/finder": "^6.4",
"symfony/console": "^6.4",
"jolicode/castor": "^0.13.1"
}
}

0 comments on commit d816d14

Please sign in to comment.