Completed
Push — master ( 9da0fd...2ae4f2 )
by Tomáš
9s
created

SculpinApplication   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 82.93%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 119
ccs 34
cts 41
cp 0.8293
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A runCommand() 0 15 2
A processCommand() 0 19 1
A loadSourcesFromSourceDirectory() 0 5 1
A findFilesInSourceDirectory() 0 10 1
A loadLayoutsToLatteLoader() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Application;
11
12
use Nette\Utils\Finder;
13
use SplFileInfo;
14
use Symplify\PHP7_Sculpin\HttpServer\HttpServer;
15
use Symplify\PHP7_Sculpin\Output\FileSystemWriter;
16
use Symplify\PHP7_Sculpin\Application\Command\RunCommand;
17
use Symplify\PHP7_Sculpin\Configuration\Configuration;
18
use Symplify\PHP7_Sculpin\Renderable\Latte\DynamicStringLoader;
19
use Symplify\PHP7_Sculpin\Renderable\RenderableFilesProcessor;
20
use Symplify\PHP7_Sculpin\Source\SourceFileStorage;
21
22
final class SculpinApplication
23
{
24
    /**
25
     * @var SourceFileStorage
26
     */
27
    private $sourceFileStorage;
28
29
    /**
30
     * @var Configuration
31
     */
32
    private $configuration;
33
34
    /**
35
     * @var FileSystemWriter
36
     */
37
    private $fileSystemWriter;
38
39
    /**
40
     * @var RenderableFilesProcessor
41
     */
42
    private $renderableFilesProcessor;
43
44
    /**
45
     * @var DynamicStringLoader
46
     */
47
    private $dynamicStringLoader;
48
49
    /**
50
     * @var HttpServer
51
     */
52
    private $httpServer;
53
54
    /**
55
     * @var string
56
     */
57
    private $sinceTime;
58
59 1
    public function __construct(
60
        SourceFileStorage $sourceFileStorage,
61
        Configuration $configuration,
62
        FileSystemWriter $fileSystemWriter,
63
        RenderableFilesProcessor $renderableFilesProcessor,
64
        DynamicStringLoader $dynamicStringLoader,
65
        HttpServer $httpServer
66
    ) {
67 1
        $this->sourceFileStorage = $sourceFileStorage;
68 1
        $this->configuration = $configuration;
69 1
        $this->fileSystemWriter = $fileSystemWriter;
70 1
        $this->renderableFilesProcessor = $renderableFilesProcessor;
71 1
        $this->dynamicStringLoader = $dynamicStringLoader;
72 1
        $this->httpServer = $httpServer;
73 1
        $this->sinceTime = '1970-01-01T00:00:00Z';
74 1
    }
75
76 1
    public function runCommand(RunCommand $runCommand)
77
    {
78 1
        $this->processCommand($runCommand);
79
80 1
        if ($runCommand->isRunServer()) {
81
            $this->httpServer->init();
82
83
            $this->httpServer->addPeriodicTimer(1, function () use ($runCommand) {
84
                clearstatcache();
85
                $this->processCommand($runCommand);
86
            });
87
88
            $this->httpServer->run();
89
        }
90 1
    }
91
92 1
    private function processCommand(RunCommand $runCommand)
93
    {
94 1
        $this->loadSourcesFromSourceDirectory($runCommand->getSourceDirectory());
95
96
        // 1. copy static files
97 1
        $this->fileSystemWriter->copyStaticFiles($this->sourceFileStorage->getStaticFiles());
98
99
        // 2. collect configuration
100 1
        $this->configuration->loadOptionsFromFiles($this->sourceFileStorage->getConfigurationFiles());
101
102
        // 3. collect layouts
103 1
        $this->loadLayoutsToLatteLoader($this->sourceFileStorage->getLayoutFiles());
104
105
        // 4. completely process post
106 1
        $this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getPostFiles());
107
108
        // 5. render files
109 1
        $this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getRenderableFiles());
110 1
    }
111
112 1
    private function loadSourcesFromSourceDirectory(string $sourceDirectory)
113
    {
114 1
        $finder = $this->findFilesInSourceDirectory($sourceDirectory);
115 1
        $this->sourceFileStorage->loadSourcesFromFinder($finder);
116 1
    }
117
118 1
    private function findFilesInSourceDirectory(string $sourceDirectory) : Finder
119
    {
120 1
        $sinceTimeLast = $this->sinceTime;
121
122 1
        $this->sinceTime = date('c');
123
124 1
        return Finder::findFiles('*')
125 1
            ->from($sourceDirectory)
126 1
            ->date('>=', $sinceTimeLast);
127
    }
128
129
    /**
130
     * @param SplFileInfo[] $layoutFiles
131
     */
132 1
    private function loadLayoutsToLatteLoader(array $layoutFiles)
133
    {
134 1
        foreach ($layoutFiles as $layoutFile) {
135
            $name = $layoutFile->getBasename('.'.$layoutFile->getExtension());
136
            $content = file_get_contents($layoutFile->getRealPath());
137
            $this->dynamicStringLoader->addTemplate($name, $content);
138
        }
139 1
    }
140
}
141