|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCR\Shell\Config; |
| 4 | + |
| 5 | +use Symfony\Component\Filesystem\Filesystem; |
| 6 | +use PHPCR\Shell\Console\Helper\ConfigHelper; |
| 7 | +use Symfony\Component\Finder\Finder; |
| 8 | +use Symfony\Component\Yaml\Yaml; |
| 9 | +use PHPCR\Shell\Config\Exception\FileExistsException; |
| 10 | + |
| 11 | +class ProfileLoader |
| 12 | +{ |
| 13 | + const DIR_PROFILE = 'profiles'; |
| 14 | + |
| 15 | + protected $config; |
| 16 | + protected $filesystem; |
| 17 | + |
| 18 | + public function __construct(ConfigHelper $config, Filesystem $filesystem = null) |
| 19 | + { |
| 20 | + $this->config = $config; |
| 21 | + $this->filesystem = $filesystem ? : new Filesystem; |
| 22 | + } |
| 23 | + |
| 24 | + protected function getProfileDir() |
| 25 | + { |
| 26 | + $dir = sprintf('%s/%s', $this->config->getConfigDir(), self::DIR_PROFILE);; |
| 27 | + |
| 28 | + return $dir; |
| 29 | + } |
| 30 | + |
| 31 | + public function getProfilePath($name) |
| 32 | + { |
| 33 | + $dir = sprintf('%s/%s/%s.yml', $this->config->getConfigDir(), self::DIR_PROFILE, $name);; |
| 34 | + |
| 35 | + return $dir; |
| 36 | + } |
| 37 | + |
| 38 | + public function getProfileNames() |
| 39 | + { |
| 40 | + $dir = $this->getProfileDir(); |
| 41 | + |
| 42 | + if (false === $this->filesystem->exists($dir)) { |
| 43 | + return array(); |
| 44 | + } |
| 45 | + |
| 46 | + $files = Finder::create()->files()->name('*.yml')->in($dir); |
| 47 | + |
| 48 | + $profiles = array(); |
| 49 | + foreach ($files as $file) { |
| 50 | + $profiles[] = substr($file->getBasename(), 0, -4); |
| 51 | + } |
| 52 | + |
| 53 | + sort($profiles); |
| 54 | + |
| 55 | + return $profiles; |
| 56 | + } |
| 57 | + |
| 58 | + public function loadProfile(Profile $profile) |
| 59 | + { |
| 60 | + $path = $this->getProfilePath($profile->getName()); |
| 61 | + |
| 62 | + if (!file_exists($path)) { |
| 63 | + throw new \InvalidArgumentException(sprintf('Profile "%s" does not exist, expected to find it in "%s"', |
| 64 | + $profile->getName(), $path |
| 65 | + )); |
| 66 | + } |
| 67 | + |
| 68 | + $contents = file_get_contents($path); |
| 69 | + $data = Yaml::parse($contents); |
| 70 | + |
| 71 | + if (isset($data['transport'])) { |
| 72 | + $profile->set('transport', $data['transport']); |
| 73 | + } |
| 74 | + |
| 75 | + if (isset($data['phpcr'])) { |
| 76 | + $profile->set('phpcr', $data['phpcr']); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public function saveProfile(Profile $profile, $overwrite = false) |
| 81 | + { |
| 82 | + $profileDir = $this->getProfileDir(); |
| 83 | + $path = $this->getProfilePath($profile->getName()); |
| 84 | + |
| 85 | + if (false === $overwrite && file_exists($path)) { |
| 86 | + throw new FileExistsException(sprintf( |
| 87 | + 'Profile already exists at "%s"', $path |
| 88 | + )); |
| 89 | + } |
| 90 | + |
| 91 | + $yaml = Yaml::dump($profile->toArray()); |
| 92 | + |
| 93 | + $this->filesystem->dumpFile($path, $yaml, 0600); |
| 94 | + } |
| 95 | +} |
0 commit comments