Issues (2)

src/DeferedImageShortcodeProvider.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace ilateral\SilverStripe\DeferedImages;
4
5
use SilverStripe\Dev\Debug;
6
use SilverStripe\View\HTML;
7
use SilverStripe\Assets\File;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Assets\Storage\AssetStore;
11
use SilverStripe\Assets\Shortcodes\ImageShortcodeProvider;
12
13
class DeferedImageShortcodeProvider extends ImageShortcodeProvider
14
{
15
    /**
16
     * Replace"[image id=n]" shortcode with an image reference.
17
     * Permission checks will be enforced by the file routing itself.
18
     *
19
     * @param array $args Arguments passed to the parser
20
     * @param string $content Raw shortcode
21
     * @param ShortcodeParser $parser Parser
0 ignored issues
show
The type ilateral\SilverStripe\De...dImages\ShortcodeParser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
     * @param string $shortcode Name of shortcode used to register this handler
23
     * @param array $extra Extra arguments
24
     * @return string Result of the handled shortcode
25
     */
26
    public static function handle_shortcode($args, $content, $parser, $shortcode, $extra = array())
27
    {
28
        $cache = static::getCache();
29
        $cacheKey = static::getCacheKey($args);
30
31
        $item = $cache->get($cacheKey);
32
        if ($item) {
33
            /** @var AssetStore $store */
34
            $store = Injector::inst()->get(AssetStore::class);
35
            if (!empty($item['filename'])) {
36
                $store->grant($item['filename'], $item['hash']);
37
            }
38
            return $item['markup'];
39
        }
40
41
        // Find appropriate record, with fallback for error handlers
42
        $record = static::find_shortcode_record($args, $errorCode);
43
        if ($errorCode) {
44
            $record = static::find_error_record($errorCode);
45
        }
46
        if (!$record) {
47
            return null; // There were no suitable matches at all.
48
        }
49
50
        // Check if a resize is required
51
        $src = $record->Link();
52
        $micro_url = false;
53
        if ($record instanceof Image) {
54
            $width = isset($args['width']) ? $args['width'] : null;
55
            $height = isset($args['height']) ? $args['height'] : null;
56
            $hasCustomDimensions = ($width && $height);
57
            if ($hasCustomDimensions && (($width != $record->getWidth()) || ($height != $record->getHeight()))) {
58
                $resized = $record->ResizedImage($width, $height);
59
                // Make sure that the resized image actually returns an image
60
                if (!empty($resized)) {
61
                    $src = $resized->getURL();
62
                }
63
            }
64
            $micro_url = $record->MicroImage()->getURL();
65
        }
66
67
        // Build the HTML tag
68
        $attrs = array_merge(
69
            // Set overrideable defaults
70
            ['src' => '', 'alt' => $record->Title],
71
            // Use all other shortcode arguments
72
            $args,
73
            // But enforce some values
74
            ['id' => '', 'src' => $src]
75
        );
76
77
        // Clean out any empty attributes
78
        $attrs = array_filter($attrs, function ($v) {
79
            return (bool)$v;
80
        });
81
82
        if ($micro_url) {
83
            $attrs['data-src'] = $src;
84
            $attrs['src'] = $micro_url;
85
        }
86
87
        $markup = HTML::createTag('img', $attrs);
88
89
        // cache it for future reference
90
        $cache->set($cacheKey, [
91
            'markup' => $markup,
92
            'filename' => $record instanceof File ? $record->getFilename() : null,
93
            'hash' => $record instanceof File ? $record->getHash() : null,
94
        ]);
95
96
        return $markup;
97
    }
98
}