Completed
Branch Gutenberg/master (3b2a95)
by
unknown
118:45 queued 105:17
created

AssetManager::assetNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\assets;
4
5
use EventEspresso\core\domain\DomainInterface;
6
use EventEspresso\core\domain\values\assets\JavascriptAsset;
7
use EventEspresso\core\domain\values\assets\ManifestFile;
8
use EventEspresso\core\domain\values\assets\StylesheetAsset;
9
use EventEspresso\core\exceptions\InvalidDataTypeException;
10
use EventEspresso\core\exceptions\InvalidEntityException;
11
use EventEspresso\core\services\collections\DuplicateCollectionIdentifierException;
12
13
/**
14
 * Class AssetManager
15
 * Manager class for helping with adding and retrieving Asset objects from an AssetCollection
16
 *
17
 * @package EventEspresso\core\services\assets
18
 * @author  Brent Christensen
19
 * @since   4.9.62.p
20
 */
21
abstract class AssetManager implements AssetManagerInterface
22
{
23
24
    /**
25
     * @var AssetCollection $assets
26
     */
27
    protected $assets;
28
29
    /**
30
     * @var DomainInterface
31
     */
32
    protected $domain;
33
34
    /**
35
     * @var Registry $registry
36
     */
37
    protected $registry;
38
39
40
    /**
41
     * AssetRegister constructor.
42
     *
43
     * @param DomainInterface $domain
44
     * @param AssetCollection $assets
45
     * @param Registry        $registry
46
     */
47
    public function __construct(DomainInterface $domain, AssetCollection $assets, Registry $registry)
48
    {
49
        $this->domain = $domain;
50
        $this->assets = $assets;
51
        $this->registry = $registry;
52
        add_action('wp_enqueue_scripts', array($this, 'addManifestFile'), 0);
53
        add_action('admin_enqueue_scripts', array($this, 'addManifestFile'), 0);
54
        add_action('wp_enqueue_scripts', array($this, 'addAssets'), 2);
55
        add_action('admin_enqueue_scripts', array($this, 'addAssets'), 2);
56
    }
57
58
59
    /**
60
     * @since $VID:$
61
     * @return string
62
     */
63
    public function assetNamespace()
64
    {
65
        return $this->domain->assetNamespace();
66
    }
67
68
69
    /**
70
     * @return void
71
     * @throws DuplicateCollectionIdentifierException
72
     * @throws InvalidDataTypeException
73
     * @throws InvalidEntityException
74
     * @since 4.9.62.p
75
     */
76
    public function addManifestFile()
77
    {
78
        // if a manifest file has already been added for this domain, then just return
79
        if ($this->assets->has($this->domain->assetNamespace())) {
80
            return;
81
        }
82
        $asset = new ManifestFile($this->domain);
83
        $this->assets->add($asset, $this->domain->assetNamespace());
84
    }
85
86
87
    /**
88
     * @return ManifestFile[]
89
     * @since 4.9.62.p
90
     */
91
    public function getManifestFile()
92
    {
93
        return $this->assets->getManifestFiles();
94
    }
95
96
97
    /**
98
     * @param string $handle
99
     * @param string $source
100
     * @param array  $dependencies
101
     * @param bool   $load_in_footer
102
     * @return JavascriptAsset
103
     * @throws DuplicateCollectionIdentifierException
104
     * @throws InvalidDataTypeException
105
     * @throws InvalidEntityException
106
     * @since 4.9.62.p
107
     */
108
    public function addJavascript(
109
        $handle,
110
        $source,
111
        array $dependencies = array(),
112
        $load_in_footer = true
113
    ) {
114
        $asset = new JavascriptAsset(
115
            $handle,
116
            $source,
117
            $dependencies,
118
            $load_in_footer,
119
            $this->domain
120
        );
121
        $this->assets->add($asset, $handle);
122
        return $asset;
123
    }
124
125
126
127
    /**
128
     * @param string $handle
129
     * @param string $source
130
     * @param array  $dependencies
131
     * @param string $media
132
     * @return StylesheetAsset
133
     * @throws DuplicateCollectionIdentifierException
134
     * @throws InvalidDataTypeException
135
     * @throws InvalidEntityException
136
     * @since 4.9.62.p
137
     */
138
    public function addStylesheet(
139
        $handle,
140
        $source,
141
        array $dependencies = array(),
142
        $media = 'all'
143
    ) {
144
        $asset = new StylesheetAsset(
145
            $handle,
146
            $source,
147
            $dependencies,
148
            $this->domain,
149
            $media
150
        );
151
        $this->assets->add($asset, $handle);
152
        return $asset;
153
    }
154
155
156
    /**
157
     * @param string $handle
158
     * @return bool
159
     * @since 4.9.62.p
160
     */
161
    public function enqueueAsset($handle)
162
    {
163
        if ($this->assets->has($handle)) {
164
            $asset = $this->assets->get($handle);
165
            if ($asset->isRegistered()) {
166
                $asset->enqueueAsset();
167
                return true;
168
            }
169
        }
170
        return false;
171
    }
172
}
173