-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d816d14
Showing
5 changed files
with
2,960 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.castor.stub.php | ||
/twig-include-syntaxe* | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.