-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathProfileLoader.php
More file actions
113 lines (87 loc) · 2.9 KB
/
ProfileLoader.php
File metadata and controls
113 lines (87 loc) · 2.9 KB
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
<?php
/*
* This file is part of the PHPCR Shell package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PHPCR\Shell\Config;
use PHPCR\Shell\Config\Exception\FileExistsException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
class ProfileLoader
{
const DIR_PROFILE = 'profiles';
protected $config;
protected $filesystem;
public function __construct(ConfigManager $config, ?Filesystem $filesystem = null)
{
$this->config = $config;
$this->filesystem = $filesystem ?: new Filesystem();
}
protected function getProfileDir()
{
$dir = sprintf('%s/%s', $this->config->getConfigDir(), self::DIR_PROFILE);
return $dir;
}
public function getProfilePath($name)
{
$dir = sprintf('%s/%s/%s.yml', $this->config->getConfigDir(), self::DIR_PROFILE, $name);
return $dir;
}
public function getProfileNames()
{
$dir = $this->getProfileDir();
if (false === $this->filesystem->exists($dir)) {
return [];
}
$files = Finder::create()->files()->name('*.yml')->in($dir);
$profiles = [];
foreach ($files as $file) {
$profiles[] = substr($file->getBasename(), 0, -4);
}
sort($profiles);
return $profiles;
}
public function loadProfile(Profile $profile)
{
$path = $this->getProfilePath($profile->getName());
if (!file_exists($path)) {
throw new \InvalidArgumentException(sprintf(
'Profile "%s" does not exist, expected to find it in "%s"',
$profile->getName(),
$path
));
}
$contents = file_get_contents($path);
$data = Yaml::parse($contents);
if (isset($data['transport'])) {
$profile->set('transport', $data['transport']);
}
$profileWorkspace = $profile->get('phpcr', 'workspace');
if (isset($data['phpcr'])) {
$profile->set('phpcr', $data['phpcr']);
}
// workspace argument overrides profile workspace
if ($profileWorkspace && $profileWorkspace !== 'default') {
$profile->set('phpcr', 'workspace', $profileWorkspace);
}
}
public function saveProfile(Profile $profile, $overwrite = false)
{
$profileDir = $this->getProfileDir();
$path = $this->getProfilePath($profile->getName());
if (false === $overwrite && file_exists($path)) {
throw new FileExistsException(sprintf(
'Profile already exists at "%s"',
$path
));
}
$yaml = Yaml::dump($profile->toArray());
$this->filesystem->dumpFile($path, $yaml, 0600);
}
}