1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2019 Dynamika Web |
4
|
|
|
* @link https://github.com/dynamikaweb/yii2-citta-api |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
6
|
|
|
*/ |
7
|
|
|
namespace dynamikaweb\api; |
8
|
|
|
|
9
|
|
|
use Yii; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* @author Rodrigo Dornelles <[email protected]> <[email protected]> |
14
|
|
|
* @version 0.1 (28/04/2020) |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class CittaApi extends \yii\base\Model |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param string $url_reference |
21
|
|
|
* @param object $instance |
22
|
|
|
*/ |
23
|
|
|
private static $url_reference; |
24
|
|
|
private static $instance; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* __construct |
28
|
|
|
* |
29
|
|
|
* @see Sigleton Pattern |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
|
|
private function __construct ( $config = [] ) |
33
|
|
|
{ |
34
|
|
|
parent::__construct( $config ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* url |
39
|
|
|
* |
40
|
|
|
* @see Sigleton Pattern |
41
|
|
|
* |
42
|
|
|
* @param array|void $url_reference |
43
|
|
|
* @return object self |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
|
|
public static function url($uri = null) |
47
|
|
|
{ |
48
|
|
|
// change API URI |
49
|
|
|
if($uri !== null){ |
50
|
|
|
self::$url_reference = $uri; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if(self::$instance === null){ |
54
|
|
|
self::$instance = new self; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return self::$instance; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* request |
62
|
|
|
* |
63
|
|
|
* dataProvide |
64
|
|
|
* |
65
|
|
|
* @param array|string $uri |
66
|
|
|
* @return array|void $dataProviderParams |
67
|
|
|
* |
68
|
|
|
* @throws CittaException |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
|
|
public static function request($uri, $dataProviderParams = []) |
72
|
|
|
{ |
73
|
|
|
$curl = new \Curl\Curl(); |
74
|
|
|
$redirects_count = 0; |
75
|
|
|
|
76
|
|
|
// base URI reference |
77
|
|
|
if (!self::$url_reference){ |
78
|
|
|
throw new CittaException('URI Reference Error'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// consult URI |
82
|
|
|
if (is_array($uri)){ |
83
|
|
|
$curl->get(self::$url_reference.\yii\helpers\Url::to($uri)); |
|
|
|
|
84
|
|
|
|
85
|
|
|
} else if (is_string($uri)){ |
|
|
|
|
86
|
|
|
$curl->get(self::$url_reference."/{$uri}"); |
87
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
throw new CittaException('URI Type Error'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
// redirect URI |
94
|
|
|
while (300 <= $curl->http_status_code && $curl->http_status_code < 400){ |
|
|
|
|
95
|
|
|
// probably loop |
96
|
|
|
if (++$redirects_count > 10){ |
97
|
|
|
throw new CittaException('Too many redirects'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
foreach ($curl->response_headers as $header) { |
|
|
|
|
101
|
|
|
// ignore header |
102
|
|
|
if (strpos($header, 'Location:') === false){ |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// try new uri |
107
|
|
|
$curl->get(strtr($header,[ |
108
|
|
|
'Location: ' => '', |
109
|
|
|
'location: ' => '', |
110
|
|
|
'Location:' => '', |
111
|
|
|
'location:' => '' |
112
|
|
|
]) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// resquest error |
118
|
|
|
if ($curl->error && $curl->http_status_code){ |
|
|
|
|
119
|
|
|
throw new CittaException("HTTP Status {$curl->http_status_code} Error"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// curl error |
123
|
|
|
if ($curl->error){ |
124
|
|
|
throw new CittaException($curl->error_message); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// return response data |
128
|
|
|
return new \yii\data\ArrayDataProvider( |
|
|
|
|
129
|
|
|
\yii\Helpers\ArrayHelper::merge( |
|
|
|
|
130
|
|
|
['allModels' => \yii\helpers\Json::decode($curl->response, true)], |
|
|
|
|
131
|
|
|
$dataProviderParams |
132
|
|
|
) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths