|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Services; |
|
4
|
|
|
|
|
5
|
|
|
use \Craft; |
|
6
|
|
|
use craft\base\VolumeInterface; |
|
7
|
|
|
use craft\volumes\Local; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Schematic Asset Sources Service. |
|
11
|
|
|
* |
|
12
|
|
|
* Sync Craft Setups. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Nerds & Company |
|
15
|
|
|
* @copyright Copyright (c) 2015-2018, Nerds & Company |
|
16
|
|
|
* @license MIT |
|
17
|
|
|
* |
|
18
|
|
|
* @see http://www.nerds.company |
|
19
|
|
|
*/ |
|
20
|
|
|
class Volumes extends Base |
|
21
|
|
|
{ |
|
22
|
|
|
//============================================================================================================== |
|
23
|
|
|
//================================================ EXPORT ==================================================== |
|
24
|
|
|
//============================================================================================================== |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get all asset transforms |
|
28
|
|
|
* |
|
29
|
|
|
* @return VolumeInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function getRecords() |
|
32
|
|
|
{ |
|
33
|
|
|
return Craft::$app->volumes->getAllVolumes(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
//============================================================================================================== |
|
37
|
|
|
//================================================ IMPORT ==================================================== |
|
38
|
|
|
//============================================================================================================== |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Import asset volumes. |
|
43
|
|
|
* |
|
44
|
|
|
* @TODO Export volume class |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $volumeDefinitions |
|
47
|
|
|
* @param bool $force |
|
48
|
|
|
* |
|
49
|
|
|
* @return Result |
|
50
|
|
|
*/ |
|
51
|
|
|
public function import(array $volumeDefinitions, $force = false) |
|
52
|
|
|
{ |
|
53
|
|
|
$recordsByHandle = []; |
|
54
|
|
|
foreach ($this->getRecords() as $record) { |
|
55
|
|
|
$recordsByHandle[$record->handle] = $record; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
foreach ($volumeDefinitions as $handle => $definition) { |
|
59
|
|
|
$record = new Local(); |
|
60
|
|
|
if (array_key_exists($handle, $recordsByHandle)) { |
|
61
|
|
|
$record = $recordsByHandle[$handle]; |
|
62
|
|
|
} |
|
63
|
|
|
$record->setAttributes($definition); |
|
64
|
|
|
if (Craft::$app->volumes->saveVolume($record)) { |
|
65
|
|
|
Craft::info('Imported volume '.$handle, 'schematic'); |
|
66
|
|
|
} else { |
|
67
|
|
|
Craft::warning('Error importing volume '.$handle, 'schematic'); |
|
68
|
|
|
foreach ($record->getErrors() as $errors) { |
|
69
|
|
|
foreach ($errors as $error) { |
|
70
|
|
|
var_dump($error); |
|
|
|
|
|
|
71
|
|
|
Craft::error($error, 'schematic'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
unset($recordsByHandle[$handle]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($force) { |
|
79
|
|
|
// Delete volumes not in definitions |
|
80
|
|
|
foreach ($recordsByHandle as $handle => $record) { |
|
81
|
|
|
Craft::info('Deleting volume '.$handle, 'schematic'); |
|
82
|
|
|
Craft::$app->volumes->deleteVolume($record); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: