Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Templates
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getStubs
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFirstStub
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDest
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
4
 templatePath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace San\Crud\Generators;
4
5use San\Crud\Utils\FileUtils;
6use San\Crud\Utils\TextUtils;
7
8class Templates {
9    public function __construct(public string $path) {
10        if (!is_dir($path)) {
11            throw new \Exception("Templates path not found: $path");
12        }
13    }
14
15    public function getStubs(string $suffix) {
16        $files = FileUtils::getFiles("$this->path/stubs/$suffix", ['php']);
17        return array_values(array_filter($files, fn($file) => !\str_contains($file, 'partials')));
18    }
19
20    public function getFirstStub(string $suffix) {
21        $stubs = $this->getStubs($suffix);
22        return $stubs[0] ?? NULL;
23    }
24
25    public function getDest(string $type, mixed $stub, array $replacements = [], string $suffix = '') {
26        $dest = \str_contains($type, 'views/') ? resource_path('views') . "/$suffix" : ($type === 'routes'  ? base_path("routes/crud") : ($type === 'Controllers' ? app_path('Http/Controllers') : app_path($type)));
27        $str = sprintf('%s/%s', $dest, basename($stub));
28        return TextUtils::replaceBlanks($str, $replacements);
29    }
30
31    public function templatePath() {
32        return $this->path;
33    }
34}