XOOPS  2.6.0
Manager.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Core\Service;
13 
14 use Xoops\Core\Yaml;
15 
28 class Manager
29 {
34  const MODE_EXCLUSIVE = 1;
35 
41  const MODE_CHOICE = 2;
42 
48  const MODE_PREFERENCE = 4;
49 
54  const MODE_MULTIPLE = 8;
55 
59  const PRIORITY_SELECTED = 0;
60  const PRIORITY_HIGH = 1;
61  const PRIORITY_MEDIUM = 5;
62  const PRIORITY_LOW = 9;
63 
69  protected $services = array();
70 
77  protected $providerPrefs = null;
78 
82  private $providerPrefsFilename = 'var/configs/system_provider_prefs.yml';
83 
87  private $providerPrefsCacheKey = 'system/provider/prefs';
88 
92  protected function __construct()
93  {
94  $this->providerPrefs = $this->readProviderPrefs();
95  }
96 
102  public static function getInstance()
103  {
104  static $instance = false;
105 
106  if (!$instance) {
107  $instance = new Manager();
108  }
109 
110  return $instance;
111  }
112 
118  public function readYamlProviderPrefs()
119  {
121 
122  $providerPrefs = array();
123 
124  try {
125  $file = $xoops->path($this->providerPrefsFilename);
126  if (file_exists($file)) {
128  }
129  if (empty($providerPrefs)) {
130  $providerPrefs = array();
131  }
132  } catch (\Exception $e) {
133  $xoops->events()->triggerEvent('core.exception', $e);
134  $providerPrefs = array();
135  }
136  return $providerPrefs;
137  }
138 
144  protected function readProviderPrefs()
145  {
147  $providerPrefs = $xoops->cache()->cacheRead(
148  $this->providerPrefsCacheKey,
149  array($this, 'readYamlProviderPrefs')
150  );
151  return $providerPrefs;
152  }
153 
162  protected function saveProviderPrefs($providerPrefs)
163  {
164  if (is_array($providerPrefs)) {
166  try {
167  Yaml::save($providerPrefs, $xoops->path($this->providerPrefsFilename));
168  $xoops->cache()->write($this->providerPrefsCacheKey, $providerPrefs);
169  } catch (\Exception $e) {
170  $xoops->events()->triggerEvent('core.exception', $e);
171  }
172  }
173  }
174 
186  public function saveChoice($service, $choices)
187  {
188  // read current preferences
189  $prefs = $this->readProviderPrefs();
190  // replace prefs for selected service
191  $prefs[$service] = $choices;
192  // save the changes
193  $this->saveProviderPrefs($prefs);
194  // apply to current manager instance
195  $this->registerChoice($service, $choices);
196  }
197 
209  public function registerChoice($service, $choices)
210  {
211  $provider = $this->locate($service);
212  $providers = $provider->getRegistered();
213  foreach ($providers as $p) {
214  $name = strtolower($p->getName());
215  if (isset($choices[$name])) {
216  $p->setPriority($choices[$name]);
217  }
218  }
219  $provider->sortProviders();
220  }
221 
233  public function listChoices($service)
234  {
235  $providers = $this->locate($service)->getRegistered();
236  return $providers;
237  }
238 
246  public function locate($service)
247  {
248  $service = strtolower($service);
249  if (isset($this->services[$service])) {
250  // service already located
251  $provider = $this->services[$service];
252  } else {
254  $provider = new Provider($this, $service);
255  $event = 'core.service.locate.' . $service;
256  // locate service provider(s)
257  // In response to trigger message, the contract implementor should register()
258  $xoops->events()->triggerEvent($event, $provider);
259  // get reference to the list of providers and prioritize it.
260  $registered=$provider->getRegistered();
261  if (count($registered)) {
262  $choices = isset($this->providerPrefs[$service]) ? $this->providerPrefs[$service] : array();
263  foreach ($registered as $p) {
264  $name = strtolower($p->getName());
265  if (isset($choices[$name])) {
266  $p->setPriority($choices[$name]);
267  }
268  }
269  $provider->sortProviders();
270  } else {
271  // replace with a null provider since no contract implementers were
272  $provider = new NullProvider($this, $service);
273  }
274  $this->services[$service] = $provider;
275  }
276 
277  return $provider;
278  }
279 }
registerChoice($service, $choices)
Definition: Manager.php:209
static getInstance()
Definition: Xoops.php:160
if(!isset($xoops->paths[$path_type])) if($path_type== 'var') $file
Definition: browse.php:55
saveProviderPrefs($providerPrefs)
Definition: Manager.php:162
$xoops
Definition: admin.php:25
static save($var, $yamlFile, $inline=4, $indent=4)
Definition: Yaml.php:109
saveChoice($service, $choices)
Definition: Manager.php:186
static read($yamlFile)
Definition: Yaml.php:87