4 * This file is a part of dflydev/dot-access-configuration.
6 * (c) Dragonfly Development Inc.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Dflydev\DotAccessConfiguration;
14 use Dflydev\DotAccessData\Data;
15 use Dflydev\PlaceholderResolver\PlaceholderResolverInterface;
16 use Dflydev\PlaceholderResolver\RegexPlaceholderResolver;
18 abstract class AbstractConfiguration implements ConfigurationInterface
20 private $placeholderResolver;
22 private $exportIsDirty = true;
23 private $resolvedExport;
28 public function getRaw($key)
30 return $this->data()->get($key);
36 public function get($key)
38 $value = $this->getRaw($key);
39 if (is_object($value)) {
42 $this->resolveValues($value);
50 public function set($key, $value = null)
52 $this->exportIsDirty = true;
54 return $this->data()->set($key, $value);
60 public function append($key, $value = null)
62 $this->exportIsDirty = true;
64 return $this->data()->append($key, $value);
70 public function exportRaw()
72 return $this->data()->export();
78 public function export()
80 if ($this->exportIsDirty) {
81 $this->resolvedExport = $this->data()->export();
82 $this->resolveValues($this->resolvedExport);
83 $this->exportIsDirty = false;
86 return $this->resolvedExport;
92 public function exportData()
94 return new Data($this->export());
100 public function importRaw($imported = null, $clobber = true)
102 $this->exportIsDirty = true;
104 if (null !== $imported) {
105 $this->data()->import($imported, $clobber);
112 public function import(ConfigurationInterface $imported, $clobber = true)
114 return $this->importRaw($imported->exportRaw(), $clobber);
120 public function resolve($value = null)
122 if (null === $value) {
126 return $this->placeholderResolver()->resolvePlaceholder($value);
132 public function setPlaceholderResolver(PlaceholderResolverInterface $placeholderResolver)
134 $this->placeholderResolver = $placeholderResolver;
142 * For objects, do nothing. For strings, resolve placeholder.
143 * For arrays, call resolveValues() on each item.
145 * @param mixed $input
147 protected function resolveValues(&$input = null)
149 if (is_array($input)) {
150 foreach ($input as $idx => $value) {
151 $this->resolveValues($value);
152 $input[$idx] = $value;
155 if (!is_object($input)) {
156 $input = $this->placeholderResolver()->resolvePlaceholder($input);
166 protected function data()
168 if (null === $this->data) {
169 $this->data = new Data;
176 * Placeholder Resolver
178 * @return PlaceholderResolverInterface
180 protected function placeholderResolver()
182 if (null === $this->placeholderResolver) {
183 $this->placeholderResolver = new RegexPlaceholderResolver(new ConfigurationDataSource($this), '%', '%');
186 return $this->placeholderResolver;