-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathProvider.php
142 lines (137 loc) · 4.91 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Flipside;
/**
* Provider class
*
* This file describes the Provider Singleton
*
* PHP version 5 and 7
*
* @author Patrick Boyd / [email protected]
* @copyright Copyright (c) 2015, Austin Artistic Reconstruction
* @license http://www.apache.org/licenses/ Apache 2.0 License
*/
/**
* A Singleton class to abstract access to many different methods.
*
* This class is an abstraction to different methods of doing something. For example,
* the \EmailProvider abstracts access to Amazon SES or SMTP methods of sending email.
*/
class Provider extends Singleton
{
/** The methods loaded by the provider */
protected $methods;
/**
* Get the method object corresponding to the name
*
* @param string $methodName The class name of the method to get the instance for
*
* @return boolean|stdClass The specified method class instance or false if it is not loaded
*/
public function getMethodByName($methodName)
{
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
if(strcasecmp(get_class($this->methods[$i]), $methodName) === 0)
{
return $this->methods[$i];
}
}
return false;
}
/**
* Calls the indicated function on each method and add the result
*
* @param string $functionName The function to call
* @param string $checkField A field to check if it is set a certain way before calling the function
* @param mixed $checkValue The value that field should be set to to not call the function
*
* @return integer The added returnValue
*/
protected function addFromEach($functionName, $checkField = false, $checkValue = false)
{
$retCount = 0;
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
if($checkField !== false)
{
if($this->methods[$i]->{$checkField} === $checkValue)
{
continue;
}
}
$res = call_user_func(array($this->methods[$i], $functionName));
$retCount += $res;
}
return $retCount;
}
/**
* Calls the indicated function on each method
*
* @param string $functionName The function to call
* @param array $args The arguments for the function
* @param boolean|string $checkField A field to check if it is set a certain way before calling the function
* @param mixed $checkValue The value that field should be set to to not call the function
* @param callable $resFunction Function to call on the result, otherwise the function will return on the first non-false result
*
* @return Auth\Group|Auth\User|false The merged returnValue
*/
protected function callOnEach($functionName, $args, $checkField = false, $checkValue = false, $resFunction = null)
{
$ret = false;
$count = count($this->methods);
for($i = 0; $i < $count; $i++)
{
if($checkField !== false)
{
if($this->methods[$i]->{$checkField} === $checkValue)
{
continue;
}
}
$res = call_user_func_array(array($this->methods[$i], $functionName), $args);
if($resFunction !== null)
{
call_user_func_array($resFunction, array(&$ret, $res));
continue;
}
if($res !== false)
{
return $res;
}
}
return $ret;
}
/**
* Calls the indicated function on the specified method or all methods if false
*
* @param string|boolean $methodName The method to call the function on, or false to call on all functions
* @param string $functionName The function to call
* @param array $args The arguments for the function
* @param boolean|string $checkField A field to check if it is set a certain way before calling the function
* @param mixed $checkValue The value that field should be set to to not call the function
* @param callable $resFunction Function to call on the result, otherwise the function will return on the first non-false result
*
* @return mixed The return value
*/
protected function callFunction($methodName, $functionName, $args, $checkField = false, $checkValue = false, $resFunction = null)
{
if($methodName === false)
{
return $this->callOnEach($functionName, $args, $checkField, $checkValue, $resFunction);
}
if(is_string($methodName))
{
$method = $this->getMethodByName($methodName);
if($method === false)
{
return false;
}
return call_user_func_array(array($method, $functionName), $args);
}
return false;
}
}
/* vim: set tabstop=4 shiftwidth=4 expandtab: */