-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigDumper.php
165 lines (148 loc) · 4.03 KB
/
ConfigDumper.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
<?php
/**
* ConfigDumper.php
*
* PHP Version 5
*
* @category magento2
* @package magento2
* @author David Verholen <[email protected]>
* @license http://opensource.org/licenses/OSL-3.0 OSL-3.0
* @link http://github.com/davidverholen
*/
namespace DavidVerholen\DynamicComponentRegistry\Observer;
use DavidVerholen\DynamicComponentRegistry\Model\Serializable\ConfigFactory;
use DavidVerholen\DynamicComponentRegistry\Model\SerializerFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\Observer as ObserverEventObject;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Filesystem\Directory\WriteFactory as DirectoryWriteFactory;
use Magento\Framework\Filesystem\Driver\File;
/**
* Class ConfigDumper
*
* @category magento2
* @package DavidVerholen\DynamicComponentRegistry\Observer
* @author David Verholen <[email protected]>
* @license http://opensource.org/licenses/OSL-3.0 OSL-3.0
* @link http://github.com/davidverholen
*/
class ConfigDumper implements ObserverInterface
{
/**
* @var SerializerFactory
*/
private $serializerFactory;
/**
* @var ConfigFactory
*/
private $configFactory;
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @var DirectoryWriteFactory
*/
private $directoryWriteFactory;
/**
* @var File
*/
private $fileDriver;
/**
* ConfigDumper constructor.
*
* @param SerializerFactory $serializerFactory
* @param ConfigFactory $configFactory
* @param DirectoryList $directoryList
* @param DirectoryWriteFactory $directoryWriteFactory
* @param File $fileDriver
*/
public function __construct(
SerializerFactory $serializerFactory,
ConfigFactory $configFactory,
DirectoryList $directoryList,
DirectoryWriteFactory $directoryWriteFactory,
File $fileDriver
) {
$this->serializerFactory = $serializerFactory;
$this->configFactory = $configFactory;
$this->directoryList = $directoryList;
$this->directoryWriteFactory = $directoryWriteFactory;
$this->fileDriver = $fileDriver;
}
/**
* @param Observer $observer
*
* @return void
*/
public function execute(ObserverEventObject $observer)
{
$this->dumpConfigToFile();
}
/**
* dumpConfigToFile
*
* @return boolean
* @throws \Magento\Framework\Exception\FileSystemException
*/
protected function dumpConfigToFile()
{
if (false === $this->createConfigDir()) {
return false;
}
if (false === $this->fileDriver->isExists($this->getConfigFilePath())) {
$this->fileDriver->touch($this->getConfigFilePath());
}
return $this->fileDriver->filePutContents(
$this->getConfigFilePath(),
$this->getConfigJson()
) > 0;
}
/**
* createConfigDir
*
* @return bool
*/
protected function createConfigDir()
{
return $this->directoryWriteFactory
->create($this->directoryList->getPath(DirectoryList::VAR_DIR))
->create(ConfigFactory::CONFIG_DIR);
}
/**
* getConfigFilePath
*
* @return string
*/
protected function getConfigFilePath()
{
return implode(DIRECTORY_SEPARATOR, [
$this->directoryList->getPath(DirectoryList::VAR_DIR),
ConfigFactory::CONFIG_DIR,
ConfigFactory::CONFIG_FILE_NAME
]);
}
/**
* getConfigJson
*
* @return string
*/
protected function getConfigJson()
{
return $this->getSerializer()->serialize(
$this->configFactory->create(),
ConfigFactory::CONFIG_FORMAT
);
}
/**
* getSerializer
*
* @return \JMS\Serializer\Serializer
*/
protected function getSerializer()
{
return $this->serializerFactory->get();
}
}