|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/signals |
|
7
|
|
|
* @license AGPL, Commercial |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Maslosoft\Signals\Builder\IO; |
|
14
|
|
|
|
|
15
|
|
|
use Maslosoft\Cli\Shared\Helpers\PhpExporter; |
|
16
|
|
|
use Maslosoft\Signals\Interfaces\BuilderIOInterface; |
|
17
|
|
|
use Maslosoft\Signals\Signal; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PhpFileOutput |
|
21
|
|
|
* |
|
22
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
23
|
|
|
*/ |
|
24
|
|
|
class PhpFile implements BuilderIOInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Generated path. |
|
29
|
|
|
* This is path, where signals definition will be stored. |
|
30
|
|
|
* Path is relative to project root. |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
public $generatedPath = 'generated'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* File name for file containing signals definitions. |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
public $configFilename = 'signals-definition.php'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Signal instance |
|
43
|
|
|
* @var Signal |
|
44
|
|
|
*/ |
|
45
|
|
|
private $signal = null; |
|
46
|
|
|
|
|
47
|
|
|
public function read() |
|
48
|
|
|
{ |
|
49
|
|
|
$file = $this->generatedPath . '/' . $this->configFilename; |
|
50
|
|
|
if (file_exists($file)) |
|
51
|
|
|
{ |
|
52
|
|
|
return (array) require $file; |
|
53
|
|
|
} |
|
54
|
|
|
else |
|
55
|
|
|
{ |
|
56
|
|
|
$this->signal->getLogger()->debug('Config file "{file}" does not exists, have you generated signals config file?', ['file' => $file]); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function setSignal(Signal $signal) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->signal = $signal; |
|
63
|
1 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function write($data) |
|
67
|
|
|
{ |
|
68
|
|
|
$path = sprintf("%s/%s", $this->generatedPath, $this->configFilename); |
|
69
|
|
|
|
|
70
|
|
|
// Use dirname here in case configFilename contains dir |
|
71
|
|
|
$dir = dirname($path); |
|
72
|
|
|
if (!is_dir($dir)) |
|
73
|
|
|
{ |
|
74
|
|
|
mkdir($dir, 0777, true); |
|
75
|
|
|
} |
|
76
|
|
|
return file_put_contents($path, PhpExporter::export($data, 'Auto generated, any changes will be lost')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|