GettextCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B fire() 0 42 5
1
<?php
2
3
namespace Finder\Console\Commands;
4
5
use File;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\View\Compilers\BladeCompiler;
9
10
class GettextCommand extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'sitec:gettext';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Compiles Blade templates into PHP for GNU gettext to be able to parse them';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @return void
30
     */
31
    public function fire()
32
    {
33
        // Set directories
34
        $inputPath = base_path('resources/views');
35
        $outputPath = storage_path('gettext');
36
37
        // Create $outputPath or empty it if already exists
38
        if (File::isDirectory($outputPath)) {
39
            File::cleanDirectory($outputPath);
40
        } else {
41
            File::makeDirectory($outputPath);
42
        }
43
44
        // Configure BladeCompiler to use our own custom storage subfolder
45
        $compiler = new BladeCompiler(new Filesystem, $outputPath);
46
        $compiled = 0;
47
48
        // Get all view files
49
        $allFiles = File::allFiles($inputPath);
50
        foreach ($allFiles as $f)
51
        {
52
            // Skip not blade templates
53
            $file = $f->getPathName();
54
            if ('.blade.php' !== substr($file, -10)) {
55
                continue;
56
            }
57
58
            // Compile the view
59
            $compiler->compile($file);
60
            $compiled++;
61
62
            // Rename to human friendly
63
            $human = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($f->getRelativePathname(), DIRECTORY_SEPARATOR));
64
            File::move($outputPath . DIRECTORY_SEPARATOR . md5($file), $outputPath . DIRECTORY_SEPARATOR . $human . '.php');
65
        }
66
67
        if ($compiled) {
68
            $this->info("$compiled files compiled.");
69
        } else {
70
            $this->error('No .blade.php files found in '.$inputPath);
71
        }
72
    }
73
}
74