1 | <?php |
||
2 | /** |
||
3 | * SEOmatic plugin for Craft CMS |
||
4 | * |
||
5 | * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, |
||
6 | * and flexible |
||
7 | * |
||
8 | * @link https://nystudio107.com |
||
9 | * @copyright Copyright (c) nystudio107 |
||
10 | */ |
||
11 | |||
12 | namespace nystudio107\seomatic\helpers; |
||
13 | |||
14 | use Craft; |
||
15 | use craft\elements\Asset; |
||
16 | use craft\fs\Temp; |
||
17 | use craft\helpers\ArrayHelper; |
||
0 ignored issues
–
show
|
|||
18 | use craft\services\ElementSources; |
||
19 | |||
20 | /** |
||
21 | * @author nystudio107 |
||
22 | * @package Seomatic |
||
23 | * @since 4.1.18 |
||
24 | */ |
||
25 | class AssetHelper |
||
26 | { |
||
27 | /** |
||
28 | * Return asset volume sources that can be accessed by the current user |
||
29 | * |
||
30 | * @return array |
||
31 | */ |
||
32 | public static function getAssetInputSources(): array |
||
33 | { |
||
34 | $sources = []; |
||
35 | foreach (Craft::$app->getElementSources()->getSources(Asset::class) as $source) { |
||
36 | if ($source['type'] !== ElementSources::TYPE_HEADING) { |
||
37 | $sources[] = $source['key']; |
||
38 | } |
||
39 | } |
||
40 | |||
41 | $userService = Craft::$app->getUser(); |
||
42 | $volumesService = Craft::$app->getVolumes(); |
||
43 | return ArrayHelper::where($sources, function(string $source) use ($volumesService, $userService) { |
||
44 | // If it’s not a volume folder, let it through |
||
45 | if (!str_starts_with($source, 'volume:')) { |
||
46 | return true; |
||
47 | } |
||
48 | // Only show it if they have permission to view it, or if it's the temp volume |
||
49 | $volumeUid = explode(':', $source)[1]; |
||
50 | if ($userService->checkPermission("viewAssets:$volumeUid")) { |
||
51 | return true; |
||
52 | } |
||
53 | $volume = $volumesService->getVolumeByUid($volumeUid); |
||
54 | return $volume?->getFs() instanceof Temp; |
||
55 | }, true, true, false); |
||
56 | } |
||
57 | } |
||
58 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: