1: <?php
2:
3: namespace PHPixie\Config;
4:
5: class Formats
6: {
7: protected $formats = array();
8: protected $extensionMap = array(
9: 'php' => 'php',
10: 'json' => 'json'
11: );
12:
13: protected $classMap = array(
14: 'php' => '\PHPixie\Config\Formats\Format\PHP',
15: 'json' => '\PHPixie\Config\Formats\Format\JSON',
16: );
17:
18: public function php()
19: {
20: return $this->get('php');
21: }
22:
23: public function json()
24: {
25: return $this->get('json');
26: }
27:
28: public function getByFilename($file)
29: {
30: $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
31: $name = $this->extensionMap[$extension];
32: return $this->get($name);
33: }
34:
35: protected function get($name)
36: {
37: if(!array_key_exists($name, $this->formats)) {
38: $this->formats[$name] = $this->buildFormat($name);
39: }
40:
41: return $this->formats[$name];
42: }
43:
44: protected function buildFormat($name)
45: {
46: $class = $this->classMap[$name];
47: return new $class;
48: }
49: }