Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| TemplateLoader | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| load | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of fast-forward/dev-tools. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\DevTools\License; |
| 20 | |
| 21 | use RuntimeException; |
| 22 | |
| 23 | use function Safe\file_get_contents; |
| 24 | |
| 25 | /** |
| 26 | * Loads license template files from the filesystem. |
| 27 | * |
| 28 | * This class reads license template files from a configured templates directory. |
| 29 | * The default directory is the packaged resources/licenses folder. |
| 30 | */ |
| 31 | class TemplateLoader implements TemplateLoaderInterface |
| 32 | { |
| 33 | private string $templatesPath; |
| 34 | |
| 35 | /** |
| 36 | * Creates a new TemplateLoader instance. |
| 37 | * |
| 38 | * @param string|null $templatesPath Optional custom path to the templates directory |
| 39 | */ |
| 40 | public function __construct(?string $templatesPath = null) |
| 41 | { |
| 42 | $this->templatesPath = $templatesPath ?? \dirname(__DIR__, 2) . '/resources/licenses'; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Loads a license template file by its filename. |
| 47 | * |
| 48 | * @param string $templateFilename The filename of the template to load (e.g., "mit.txt") |
| 49 | * |
| 50 | * @return string The template content |
| 51 | * |
| 52 | * @throws RuntimeException if the template file is not found |
| 53 | */ |
| 54 | public function load(string $templateFilename): string |
| 55 | { |
| 56 | $templatePath = $this->templatesPath . '/' . $templateFilename; |
| 57 | |
| 58 | if (! file_exists($templatePath)) { |
| 59 | throw new RuntimeException(\sprintf( |
| 60 | 'License template "%s" not found in "%s"', |
| 61 | $templateFilename, |
| 62 | $this->templatesPath |
| 63 | )); |
| 64 | } |
| 65 | |
| 66 | return file_get_contents($templatePath); |
| 67 | } |
| 68 | } |