-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom Twig loader to remove last new line of a file
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
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
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of phpDocumentor. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @link https://phpdoc.org | ||
*/ | ||
|
||
namespace phpDocumentor\Guides\Twig; | ||
|
||
use Twig\Loader\FilesystemLoader; | ||
use Twig\Source; | ||
|
||
use function preg_replace; | ||
|
||
/** | ||
* A file system loader that trims the last line ending from the template | ||
* content. | ||
*/ | ||
class TrimFilesystemLoader extends FilesystemLoader | ||
{ | ||
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
|
||
/** @param string $name */ | ||
public function getSourceContext($name): Source | ||
{ | ||
// phpcs:enable | ||
$source = parent::getSourceContext($name); | ||
|
||
return new Source( | ||
preg_replace('/\R$/', '', $source->getCode()) ?? $source->getCode(), | ||
$source->getName(), | ||
$source->getPath(), | ||
); | ||
} | ||
} |