-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exporter.php
69 lines (56 loc) · 1.6 KB
/
Exporter.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
<?php
namespace Phpsog;
use Kaloa\Filesystem\PathHelper;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Phpsog\ExportEvent;
class Exporter
{
/**
*
* @var PathHelper
*/
protected $pathHelper;
/**
*
* @var EventDispatcherInterface
*/
protected $logger;
/**
*
* @param PathHelper $pathHelper
*/
public function __construct(PathHelper $pathHelper,
EventDispatcherInterface $logger)
{
$this->pathHelper = $pathHelper;
$this->logger = $logger;
}
public function mkdir($path, $rights = 0777, $recursive = true)
{
$normPath = $this->pathHelper->normalize($path);
if (strpos($normPath, getcwd()) === 0) {
$normPath = substr($normPath, strlen(getcwd()));
$normPath = ltrim($normPath, '/');
}
mkdir($path, $rights, $recursive);
$this->logger->dispatch('onMkdir', new ExportEvent('[mkdir] ' . $normPath));
}
/**
*
* @param string $exportPath
* @param string $content
*/
public function export($exportPath, $content)
{
if (!file_exists(dirname($exportPath))) {
$this->mkdir(dirname($exportPath));
}
$normPath = $this->pathHelper->normalize($exportPath);
if (strpos($normPath, getcwd()) === 0) {
$normPath = substr($normPath, strlen(getcwd()));
$normPath = ltrim($normPath, '/');
}
file_put_contents($exportPath, $content);
$this->logger->dispatch('onExport', new ExportEvent('[export] ' . $normPath));
}
}