-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ProviderManagerHelperTrait.php
169 lines (146 loc) · 4.38 KB
/
ProviderManagerHelperTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @package Joomla.API
* @subpackage com_media
*
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Media\Administrator\Provider;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Component\Media\Administrator\Adapter\AdapterInterface;
use Joomla\Component\Media\Administrator\Event\MediaProviderEvent;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Trait for classes that need adapters.
*
* @since 4.1.0
*/
trait ProviderManagerHelperTrait
{
/**
* Holds the available media file adapters.
*
* @var ProviderManager
*
* @since 4.1.0
*/
private $providerManager = null;
/**
* The default adapter name.
*
* @var string
*
* @since 4.1.0
*/
private $defaultAdapterName = null;
/**
* Return a provider manager.
*
* @return ProviderManager
*
* @since 4.1.0
*/
public function getProviderManager(): ProviderManager
{
if (!$this->providerManager) {
// Fire the event to get the results
$eventParameters = ['context' => 'AdapterManager', 'providerManager' => new ProviderManager()];
$event = new MediaProviderEvent('onSetupProviders', $eventParameters);
PluginHelper::importPlugin('filesystem');
Factory::getApplication()->triggerEvent('onSetupProviders', $event);
$this->providerManager = $event->getProviderManager();
}
return $this->providerManager;
}
/**
* Returns a provider for the given id.
*
* @param string $id
*
* @return ProviderInterface
*
* @throws \Exception
* @since 4.1.0
*/
public function getProvider(string $id): ProviderInterface
{
return $this->getProviderManager()->getProvider($id);
}
/**
* Return an adapter for the given name.
*
* @param string $name
*
* @return AdapterInterface
*
* @throws \Exception
* @since 4.1.0
*/
public function getAdapter(string $name): AdapterInterface
{
return $this->getProviderManager()->getAdapter($name);
}
/**
* Returns an array with the adapter name as key and the path of the file.
*
* @param string $path
*
* @return array
*
* @throws \Exception
* @since 4.1.0
*/
protected function resolveAdapterAndPath(string $path): array
{
$result = [];
$parts = explode(':', $path, 2);
// If we have 2 parts, we have both an adapter name and a file path
if (\count($parts) === 2) {
$result['adapter'] = $parts[0];
$result['path'] = $parts[1];
return $result;
}
if (!$this->getDefaultAdapterName()) {
throw new \InvalidArgumentException(Text::_('COM_MEDIA_ERROR_NO_ADAPTER_FOUND'));
}
// If we have less than 2 parts, we return a default adapter name
$result['adapter'] = $this->getDefaultAdapterName();
// If we have 1 part, we return it as the path. Otherwise we return a default path
$result['path'] = \count($parts) ? $parts[0] : '/';
return $result;
}
/**
* Returns the default adapter name.
*
* @return string|null
*
* @throws \Exception
*
* @since 4.1.0
*/
protected function getDefaultAdapterName(): ?string
{
if ($this->defaultAdapterName) {
return $this->defaultAdapterName;
}
$defaultAdapter = $this->getAdapter('local-' . ComponentHelper::getParams('com_media')->get('file_path', 'images'));
if (
!$defaultAdapter
&& $this->getProviderManager()->getProvider('local')
&& $this->getProviderManager()->getProvider('local')->getAdapters()
) {
$defaultAdapter = $this->getProviderManager()->getProvider('local')->getAdapters()[0];
}
if (!$defaultAdapter) {
return null;
}
$this->defaultAdapterName = 'local-' . $defaultAdapter->getAdapterName();
return $this->defaultAdapterName;
}
}