1<?php
2
3class PluginQueryExtensionManager {
4
5    private static $instance;
6    private $queryExtensions = array();
7
8    /**
9     *
10     * @return PluginQueryExtensionManager
11     */
12    public static function instance() {
13        if (!(self::$instance instanceof PluginQueryExtensionManager)) {
14            self::$instance = new self();
15        }
16        return self::$instance;
17    }
18
19    /**
20     *
21     * @param string $serviceName
22     * @param string $methodName
23     * @return array
24     */
25    public function getQueryExtensions($serviceName = null, $methodName = null) {
26        if (empty($serviceName)) {
27            return $this->queryExtensions;
28        } else {
29            if (array_key_exists($serviceName, $this->queryExtensions)) {
30                if (empty($methodName)) {
31                    return $this->queryExtensions[$serviceName];
32                } else {
33                    if (array_key_exists($methodName, $this->queryExtensions[$serviceName])) {
34                        return $this->queryExtensions[$serviceName][$methodName];
35                    } else {
36                        return array();
37                    }
38                }
39            } else {
40                return array();
41            }
42        }
43    }
44
45    /**
46     *
47     * @param array $queryExtensions
48     */
49    public function setQueryExtensions(array $queryExtensions) {
50        $this->queryExtensions = $queryExtensions;
51    }
52
53    private function __construct() {
54        $this->_init();
55    }
56
57    private function _init() {
58
59        $pluginsPath = sfConfig::get('sf_plugins_dir');
60        $directoryIterator = new DirectoryIterator($pluginsPath);
61        foreach ($directoryIterator as $fileInfo) {
62            if ($fileInfo->isDir()) {
63
64                $pluginName = $fileInfo->getFilename();
65                $configuraitonPath = $pluginsPath . '/' . $pluginName . '/config/query_extensions.yml';
66
67                if (is_file($configuraitonPath)) {
68                    $configuraiton = sfYaml::load($configuraitonPath);
69
70                    if (!is_array($configuraiton)) {
71                        continue;
72                    }
73
74                    foreach ($configuraiton as $component => $configuraitonForComponent) {
75                        if (!isset($this->queryExtensions[$component])) {
76                            $this->queryExtensions[$component] = array();
77                        }
78
79                        foreach ($configuraitonForComponent as $property => $value) {
80                            if (!isset($this->queryExtensions[$component][$property])) {
81                                $this->queryExtensions[$component][$property] = array();
82                            }
83
84                            if (is_array($value)) {
85                                foreach ($value as $k => $v) {
86                                    if (isset($this->queryExtensions[$component][$property][$k])) {
87                                        $this->queryExtensions[$component][$property][$k] = array_merge($this->queryExtensions[$component][$property][$k], $v);
88                                    } else {
89                                        $this->queryExtensions[$component][$property][$k] = $v;
90                                    }
91                                }
92                            } else {
93                                $this->queryExtensions[$component][$property][] = $value;
94                            }
95                        }
96                    }
97                }
98            }
99        }
100    }
101
102}
103
104