Completed
Pull Request — master (#75)
by Vladimir
02:42
created

Website::getCompiler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx;
9
10
use allejo\stakx\Event\BuildProcessComplete;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use allejo\stakx\Filesystem\Folder;
13
use allejo\stakx\Manager\AssetManager;
14
use allejo\stakx\Manager\ThemeManager;
15
use allejo\stakx\Templating\TemplateBridgeInterface;
16
use Highlight\Highlighter;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
class Website
20
{
21
    private $eventDispatcher;
22
    private $templateBridge;
23
    private $configuration;
24
    private $assetManager;
25
    private $compiler;
26
    private $logger;
27
28
    public function __construct(
29
        Compiler $compiler,
30
        Configuration $configuration,
31
        AssetManager $assetManager,
32
        TemplateBridgeInterface $templateBridge,
33
        EventDispatcherInterface $eventDispatcher,
34
        Logger $logger
35
    ) {
36
        $this->eventDispatcher = $eventDispatcher;
37
        $this->templateBridge = $templateBridge;
38
        $this->configuration = $configuration;
39
        $this->assetManager = $assetManager;
40
        $this->compiler = $compiler;
41
        $this->logger = $logger;
42
43
        Service::setOption('theme', $this->getConfiguration()->getTheme());
44
45
        $this->configureHighlighter();
46
    }
47
48
    public function getCompiler()
49
    {
50
        return $this->compiler;
51
    }
52
53
    /**
54
     * Compile the website.
55
     *
56
     * @return true if the website built successfully
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
57
     */
58
    public function build()
59
    {
60
        if (empty($this->getConfiguration()->getPageViewFolders()))
61
        {
62
            $this->logger->error('No PageViews were configured for this site. Check the `pageviews` key in your _config.yml.');
63
64
            return false;
65
        }
66
67
        // Configure the environment
68
        $this->createFolderStructure();
69
70
        // Our output directory
71
        $outputDirectory = new Folder($this->getConfiguration()->getTargetFolder());
72
        $outputDirectory->setTargetDirectory($this->getConfiguration()->getBaseUrl());
73
74
        // Compile everything
75
        $theme = $this->getConfiguration()->getTheme();
76
77
        $this->compiler->setTargetFolder($outputDirectory);
78
        $this->compiler->setThemeName($theme);
79
        $this->compiler->compileAll();
80
81
        if (Service::hasRunTimeFlag(RuntimeStatus::IN_PROFILE_MODE))
82
        {
83
            if (!$this->templateBridge->hasProfiler())
84
            {
85
                $this->logger->writeln('This template engine currently does not support a profiler.');
86
            }
87
            else
88
            {
89
                $profilerText = $this->templateBridge->getProfilerOutput($this->compiler);
90
                $this->logger->writeln($profilerText);
91
            }
92
        }
93
94
        // At this point, we are looking at static files to copy over meaning we need to ignore all of the files that
95
        // make up the source of a stakx website
96
        $assetsToIgnore = array_merge(
97
            Configuration::$stakxSourceFiles,
98
            $this->getConfiguration()->getExcludes()
99
        );
100
101
        //
102
        // Theme Management
103
        //
104
        if ($theme !== null)
105
        {
106
            $this->logger->notice("Looking for '${theme}' theme...");
107
108
            $tm = new ThemeManager($theme, $this->eventDispatcher, $this->logger);
109
            $tm->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore);
110
            $tm->setFolder($outputDirectory);
111
            $tm->copyFiles();
112
        }
113
114
        //
115
        // Static file management
116
        //
117
        $this->assetManager->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore);
118
        $this->assetManager->setFolder($outputDirectory);
119
        $this->assetManager->copyFiles();
120
121
        $this->eventDispatcher->dispatch(BuildProcessComplete::NAME, new BuildProcessComplete());
122
    }
123
124
    /**
125
     * @return Configuration
126
     */
127
    public function getConfiguration()
128
    {
129
        return $this->configuration;
130
    }
131
132
    /**
133
     * Prepare the Stakx environment by creating necessary cache folders.
134
     */
135
    private function createFolderStructure()
136
    {
137
        $targetDir = fs::absolutePath($this->getConfiguration()->getTargetFolder());
0 ignored issues
show
Documentation introduced by
$this->getConfiguration()->getTargetFolder() is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
139
        if (!Service::hasRunTimeFlag(RuntimeStatus::BOOT_WITHOUT_CLEAN))
140
        {
141
            fs::remove($targetDir);
0 ignored issues
show
Bug introduced by
The method remove() does not exist on allejo\stakx\Filesystem\FilesystemLoader. Did you maybe mean removeExtension()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
142
        }
143
144
        if (!Service::hasRunTimeFlag(RuntimeStatus::USING_CACHE))
145
        {
146
            fs::remove(fs::absolutePath(Configuration::CACHE_FOLDER, 'twig'));
0 ignored issues
show
Unused Code introduced by
The call to FilesystemLoader::absolutePath() has too many arguments starting with 'twig'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The method remove() does not exist on allejo\stakx\Filesystem\FilesystemLoader. Did you maybe mean removeExtension()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
147
            fs::mkdir(fs::absolutePath(fs::appendPath(Configuration::CACHE_FOLDER, 'twig')));
148
        }
149
150
        fs::mkdir($targetDir);
151
    }
152
153
    /**
154
     * Configure the Highlighter object for highlighting code blocks.
155
     */
156
    private function configureHighlighter()
157
    {
158
        $enabled = $this->getConfiguration()->isHighlighterEnabled();
159
160
        if (!$enabled)
161
        {
162
            return;
163
        }
164
165
        Service::setRuntimeFlag(RuntimeStatus::USING_HIGHLIGHTER);
166
167
        foreach ($this->getConfiguration()->getHighlighterCustomLanguages() as $lang => $path)
168
        {
169
            $fullPath = fs::absolutePath($path);
170
171
            if (!fs::exists($fullPath))
172
            {
173
                $this->logger->warning('The following language definition could not be found: {lang}', [
174
                    'lang' => $path,
175
                ]);
176
                continue;
177
            }
178
179
            Highlighter::registerLanguage($lang, $fullPath);
180
            $this->logger->debug('Loading custom language {lang} from {path}...', [
181
                'lang' => $lang,
182
                'path' => $path,
183
            ]);
184
        }
185
    }
186
}
187