XOOPS  2.6.0
Provider.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 
16 
51 class Provider
52 {
53  protected $manager = null;
54 
55  protected $service = null;
56 
57  protected $providers = array();
58 
66  {
67  $this->manager = $manager;
68  $this->service = $service;
69  }
70 
76  public function getProviderMode()
77  {
78  static $ret = null;
79 
80  if ($ret===null) {
81  if (count($this->providers)) {
82  $ret = reset($this->providers)->getMode();
83  } else {
85  }
86  }
87  return $ret;
88  }
89 
97  public function register($object)
98  {
99  // verify this is the proper type of object
100  $contract = '\Xoops\Core\Service\Contract\\' . $this->service . 'Interface';
101 
102  if (is_a($object, '\Xoops\Core\Service\AbstractContract')
103  && $object instanceof $contract
104  ) {
105  $this->providers[] = $object;
106  }
107  }
108 
114  public function &getRegistered()
115  {
116  return $this->providers;
117  }
118 
124  public function sortProviders()
125  {
126  $sortable = $this->providers;
127  $s = usort($sortable, function ($a, $b) {
128  if ($a->getPriority() != $b->getPriority()) {
129  return ($a->getPriority() > $b->getPriority()) ? 1 : -1;
130  } else {
131  return 0;
132  }
133  });
134  $this->providers = $sortable;
135  }
136 
146  public function isAvailable()
147  {
148  return true;
149  }
150 
159  public function __call($name, $arguments)
160  {
161  $mode = $this->getProviderMode();
162 
163  // for right now only one provider will be called, and it should be at the top
164  $object = reset($this->providers);
165  $method = array($object, $name);
166  $response = new Response();
167  if (is_callable($method)) {
168  try {
169  //$object->$name($response, $arguments);
170  array_unshift($arguments, $response);
171  call_user_func_array($method, $arguments);
172  } catch (\Exception $e) {
173  \XoopsPreload::getInstance()->triggerEvent('core.exception', $e);
174  $response->setSuccess(false)->addErrorMessage($e->getMessage());
175  }
176  } else {
177  $response->setSuccess(false)->addErrorMessage(sprintf('No method %s', $name));
178  }
179  return $response;
180  }
181 
190  public static function __callStatic($name, $arguments)
191  {
192  return null;
193  }
194 }
static getInstance()
Definition: Events.php:57
__construct(Manager $manager, $service)
Definition: Provider.php:65
__call($name, $arguments)
Definition: Provider.php:159
if($xoops->isUser()&&$isAdmin) $response
Definition: userinfo.php:83
service($service)
Definition: Xoops.php:250
static __callStatic($name, $arguments)
Definition: Provider.php:190