Passed
Push — assets ( 82f473...4367fa )
by Arnaud
08:49 queued 05:47
created

Asset::getStaticFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Assets;
12
13
use Cecil\Builder;
14
use Cecil\Exception\Exception;
15
use Cecil\Util;
16
17
class Asset extends AbstractAsset
18
{
19
    protected $asset = [];
20
    protected $fileLoaded = false;
21
22
    /**
23
     * Loads a file.
24
     *
25
     * @param Builder
26
     * @param string     $path
27
     * @param array|null $options
28
     */
29
    public function __construct(Builder $builder, string $path, array $options = null)
30
    {
31
        parent::__construct($builder);
32
33
        if (false === $filePath = $this->getFile($path)) {
34
            throw new Exception(sprintf('Asset file "%s" doesn\'t exist.', $path));
35
        }
36
        $fileInfo = new \SplFileInfo($filePath);
37
38
        $baseurl = (string) $this->config->get('baseurl');
39
        $base = '';
40
41
        // handles options
42
        $canonical = null;
43
        $attributs = null;
44
        extract(is_array($options) ? $options : []);
45
        if ((bool) $this->config->get('canonicalurl') || $canonical === true) {
46
            $base = rtrim($baseurl, '/');
47
        }
48
        if ($canonical === false) {
49
            $base = '';
50
        }
51
52
        // prepares properties
53
        $this->asset['path'] = $base.'/'.ltrim($path, '/');
54
        $this->asset['ext'] = $fileInfo->getExtension();
55
        $this->asset['type'] = explode('/', mime_content_type($fileInfo->getPathname()))[0];
56
        if ($this->asset['type'] == 'text') {
57
            $this->asset['content'] = file_get_contents($fileInfo->getPathname());
58
        }
59
        $this->asset['attributs'] = $attributs;
60
    }
61
62
    /**
63
     * Get a static file (in site or theme(s)) if exists or false.
64
     *
65
     * @param string $path
66
     *
67
     * @return string|false
68
     */
69
    private function getFile(string $path)
70
    {
71
        $filePath = Util::joinFile($this->config->getStaticPath(), $path);
72
        if (Util::getFS()->exists($filePath)) {
73
            return $filePath;
74
        }
75
76
        // checks in each theme
77
        foreach ($this->config->getTheme() as $theme) {
78
            $filePath = Util::joinFile($this->config->getThemeDirPath($theme, 'static'), $path);
79
            if (Util::getFS()->exists($filePath)) {
80
                return $filePath;
81
            }
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function __toString(): string
91
    {
92
        return $this->asset['path'];
93
    }
94
95
    /**
96
     * Returns as HTML tag.
97
     *
98
     * @return string
99
     */
100
    public function getHtml(): string
101
    {
102
        if ($this->asset['type'] == 'image') {
103
            return \sprintf(
104
                '<img src="%s" title="%s" alt="%s">',
105
                $this->asset['path'],
106
                $this->asset['attributs']['title'],
107
                $this->asset['attributs']['alt']
108
            );
109
        }
110
111
        switch ($this->asset['ext']) {
112
            case 'css':
113
                return \sprintf('<link rel="stylesheet" href="%s">', $this->asset['path']);
114
            case 'js':
115
                return \sprintf('<script src="%s"></script>', $this->asset['path']);
116
        }
117
118
        throw new Exception(\sprintf('%s() error: available with CSS, JS and images files only.', __FUNCTION__));
119
    }
120
121
    /**
122
     * Returns file's content.
123
     *
124
     * @return string
125
     */
126
    public function getInline(): string
127
    {
128
        if (!array_key_exists('content', $this->asset)) {
129
            throw new Exception(\sprintf('%s() error: available with CSS et JS files only.', __FUNCTION__));
130
        }
131
132
        return $this->asset['content'];
133
    }
134
}
135