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: /**
15: * Service Provider object
16: *
17: * All provider classes should extend this class, and implement the appropriate
18: * contract interface.
19: *
20: * @category Xoops\Core\Service\Provider
21: * @package Xoops\Core
22: * @author Richard Griffith <richard@geekwright.com>
23: * @copyright 2013-2015 The XOOPS Project https://github.com/XOOPS/XoopsCore
24: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25: * @version Release: 1.0
26: * @link http://xoops.org
27: *
28: * @method Response getAvatarUrl(mixed $userinfo);
29: * @method Response getAvatarEditUrl(\XoopsUser $userinfo);
30: * @method Response getImgUrl(string $value, mixed $p2=null, mixed $d3=null, mixed $d4=null, mixed $d5=null);
31: * @method Response getImgTag(string $value, mixed $d2=null, mixed $d3=null, mixed $d4=null, mixed $d5=null);
32: * @method Response startPdf();
33: * @method Response setPageOrientation(string $pageOrientation);
34: * @method Response setPageSize(string $pageSize);
35: * @method Response setBaseUnit(string $unit);
36: * @method Response setMargins(float $leftMargin, float $topMargin, float $rightMargin, float $bottomMargin);
37: * @method Response setBaseFont(string $fontFamily, string $fontStyle, float|null $fontSize);
38: * @method Response setDefaultMonospacedFont(string $monoFontFamily);
39: * @method Response setAuthor(string $pdfAuthor);
40: * @method Response setTitle(string $pdfTitle);
41: * @method Response setSubject(string $pdfSubject);
42: * @method Response setKeywords(array $pdfKeywords);
43: * @method Response addHtml(string $html);
44: * @method Response outputPdfInline(string $name);
45: * @method Response outputPdfDownload(string $name);
46: * @method Response fetchPdf();
47: * @method Response getUserRank(mixed $userinfo);
48: * @method Response getAssignableUserRankList();
49: * @method Response renderEmoji(string $buffer);
50: * @method Response getEmojiList();
51: * @method Response renderEmojiSelector(string $identifier);
52: */
53: class Provider
54: {
55: protected $manager = null;
56:
57: protected $service = null;
58:
59: /** @var AbstractContract[] $providers */
60: protected $providers = array();
61:
62: /**
63: * __construct
64: *
65: * @param Manager $manager Manager instance
66: * @param string $service service name (case sensitive)
67: */
68: public function __construct(Manager $manager, $service)
69: {
70: $this->manager = $manager;
71: $this->service = $service;
72: }
73:
74: /**
75: * getProviderMode
76: *
77: * @return Manager MODE constant
78: */
79: public function getProviderMode()
80: {
81: static $ret = null;
82:
83: if ($ret===null) {
84: if (count($this->providers)) {
85: $ret = reset($this->providers)->getMode();
86: } else {
87: return Manager::MODE_EXCLUSIVE;
88: }
89: }
90: return $ret;
91: }
92:
93: /**
94: * registerProvider - register a provider of a named service
95: *
96: * @param string $object instantiated object that provides the service
97: *
98: * @return void
99: */
100: public function register($object)
101: {
102: // verify this is the proper type of object
103: $contract = '\Xoops\Core\Service\Contract\\' . $this->service . 'Interface';
104:
105: if (is_a($object, '\Xoops\Core\Service\AbstractContract')
106: && $object instanceof $contract
107: ) {
108: $this->providers[] = $object;
109: }
110: }
111:
112: /**
113: * getRegistered - access list of registered providers
114: *
115: * @return array of registered providers managed by this instance
116: */
117: public function &getRegistered()
118: {
119: return $this->providers;
120: }
121:
122: /**
123: * sortProviders - sort providers into priority order
124: *
125: * @return void
126: */
127: public function sortProviders()
128: {
129: $sortable = $this->providers;
130: usort($sortable, function (AbstractContract $a, AbstractContract $b) {
131: if ($a->getPriority() != $b->getPriority()) {
132: return ($a->getPriority() > $b->getPriority()) ? 1 : -1;
133: } else {
134: return 0;
135: }
136: });
137: $this->providers = $sortable;
138: }
139:
140: /**
141: * isAvailable - indicate the availability of an actual provider
142: *
143: * In many cases a null provider can be called without changing the flow of the calling
144: * program. In some cases, the availability of a provider may need to be reflected in
145: * the caller, i.e. adding a UI button or menu item.
146: *
147: * @return boolean true if actual provider is available, otherwise false
148: */
149: public function isAvailable()
150: {
151: return true;
152: }
153:
154: /**
155: * All contract specified methods go here
156: *
157: * @param string $name method to call
158: * @param mixed $arguments any arguments
159: *
160: * @return Response Response
161: */
162: public function __call($name, $arguments)
163: {
164: $mode = $this->getProviderMode();
165:
166: // for right now only one provider will be called, and it should be at the top
167: $object = reset($this->providers);
168: $method = array($object, $name);
169: $response = new Response();
170: if (is_callable($method)) {
171: try {
172: //$object->$name($response, $arguments);
173: array_unshift($arguments, $response);
174: call_user_func_array($method, $arguments);
175: } catch (\Exception $e) {
176: \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
177: $response->setSuccess(false)->addErrorMessage($e->getMessage());
178: }
179: } else {
180: $response->setSuccess(false)->addErrorMessage(sprintf('No method %s', $name));
181: }
182: return $response;
183: }
184:
185: /**
186: * All static methods go here and will return null
187: *
188: * @param string $name not used
189: * @param mixed $arguments not used
190: *
191: * @return null
192: */
193: public static function __callStatic($name, $arguments)
194: {
195: return null;
196: }
197: }
198: