Completed
Push — master ( 5ce7ef...f9a211 )
by Florian
07:01 queued 05:31
created
code/SectionIO.php 1 patch
Indentation   +227 added lines, -227 removed lines patch added patch discarded remove patch
@@ -2,116 +2,116 @@  discard block
 block discarded – undo
2 2
 
3 3
 class SectionIO extends SS_Object implements Flushable
4 4
 {
5
-    private static $flush_on_dev_build = true;
6
-
7
-    private static $sitetree_flush_strategy = 'single';
8
-
9
-    private static $api_url = 'https://aperture.section.io/api/v1';
10
-    private static $account_id = '';
11
-    private static $application_id = '';
12
-    private static $environment_name = '';
13
-    private static $proxy_name = '';
14
-    private static $username = '';
15
-    private static $password = '';
16
-    private static $verify_ssl = true;
17
-
18
-    /**
19
-     * Implementation of Flushable::flush()
20
-     * Is triggered on dev/build and ?flush=1.
21
-     */
22
-    public static function flush()
23
-    {
24
-        if (Config::inst()->get('SectionIO', 'flush_on_dev_build')) {
25
-            return static::flushAll();
26
-        }
27
-
28
-        return;
29
-    }
30
-
31
-    public static function flushAll()
32
-    {
33
-        $exp = 'obj.http.x-url ~ /';
34
-
35
-        return static::performFlush($exp);
36
-    }
37
-
38
-    public static function flushImage($imageID)
39
-    {
40
-        $image = Image::get()->byID($imageID);
41
-        if ($image && $image->exists()) {
42
-            $exp = 'obj.http.x-url ~ "^/'.preg_quote($image->getFilename()).'$"'; // image itself
43
-            $exp    .= ' || obj.http.x-url ~ "^/'.preg_quote($image->Parent()->getFilename())
44
-                    .'_resampled/(.*)\-'.preg_quote($image->Name).'$"'; // resampled versions
45
-            return static::performFlush($exp);
46
-        }
47
-
48
-        return false;
49
-    }
50
-
51
-    public static function flushFile($fileID)
52
-    {
53
-        $file = File::get()->byID($fileID);
54
-        if ($file && $file->exists()) {
55
-            $exp = 'obj.http.x-url ~ "^/'.preg_quote($file->getFilename()).'$"';
56
-            return static::performFlush($exp);
57
-        }
58
-
59
-        return false;
60
-    }
61
-
62
-    public static function flushSiteTree($sitetreeID)
63
-    {
64
-        $sitetree = SiteTree::get()->byID($sitetreeID);
65
-        if ($sitetree && $sitetree->exists()) {
66
-            $strategy = Config::inst()->get('SectionIO', 'sitetree_flush_strategy');
67
-            switch ($strategy) {
68
-
69
-                case 'single':
70
-                    $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"';
71
-                    $exp .= ' && obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"';
72
-                    break;
73
-
74
-                case 'parents':
75
-                    $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"';
76
-                    $exp .= ' && (obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"';
77
-                    $parent = $sitetree->getParent();
78
-                    while ($parent && $parent->exists()) {
79
-                        $exp .= ' || obj.http.x-url ~ "^'.preg_quote($parent->Link()).'$"';
80
-                        $parent = $parent->getParent();
81
-                    }
82
-                    $exp .= ')';
83
-                    break;
84
-
85
-                case 'all':
86
-                    $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"';
87
-                    break;
88
-
89
-                case 'everyting':
90
-                default:
91
-                    $exp = 'obj.http.x-url ~ /';
92
-                    break;
93
-
94
-            }
95
-
96
-            return static::performFlush($exp);
97
-        }
98
-
99
-        return false;
100
-    }
5
+	private static $flush_on_dev_build = true;
6
+
7
+	private static $sitetree_flush_strategy = 'single';
8
+
9
+	private static $api_url = 'https://aperture.section.io/api/v1';
10
+	private static $account_id = '';
11
+	private static $application_id = '';
12
+	private static $environment_name = '';
13
+	private static $proxy_name = '';
14
+	private static $username = '';
15
+	private static $password = '';
16
+	private static $verify_ssl = true;
17
+
18
+	/**
19
+	 * Implementation of Flushable::flush()
20
+	 * Is triggered on dev/build and ?flush=1.
21
+	 */
22
+	public static function flush()
23
+	{
24
+		if (Config::inst()->get('SectionIO', 'flush_on_dev_build')) {
25
+			return static::flushAll();
26
+		}
27
+
28
+		return;
29
+	}
30
+
31
+	public static function flushAll()
32
+	{
33
+		$exp = 'obj.http.x-url ~ /';
34
+
35
+		return static::performFlush($exp);
36
+	}
37
+
38
+	public static function flushImage($imageID)
39
+	{
40
+		$image = Image::get()->byID($imageID);
41
+		if ($image && $image->exists()) {
42
+			$exp = 'obj.http.x-url ~ "^/'.preg_quote($image->getFilename()).'$"'; // image itself
43
+			$exp    .= ' || obj.http.x-url ~ "^/'.preg_quote($image->Parent()->getFilename())
44
+					.'_resampled/(.*)\-'.preg_quote($image->Name).'$"'; // resampled versions
45
+			return static::performFlush($exp);
46
+		}
47
+
48
+		return false;
49
+	}
50
+
51
+	public static function flushFile($fileID)
52
+	{
53
+		$file = File::get()->byID($fileID);
54
+		if ($file && $file->exists()) {
55
+			$exp = 'obj.http.x-url ~ "^/'.preg_quote($file->getFilename()).'$"';
56
+			return static::performFlush($exp);
57
+		}
58
+
59
+		return false;
60
+	}
61
+
62
+	public static function flushSiteTree($sitetreeID)
63
+	{
64
+		$sitetree = SiteTree::get()->byID($sitetreeID);
65
+		if ($sitetree && $sitetree->exists()) {
66
+			$strategy = Config::inst()->get('SectionIO', 'sitetree_flush_strategy');
67
+			switch ($strategy) {
68
+
69
+				case 'single':
70
+					$exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"';
71
+					$exp .= ' && obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"';
72
+					break;
73
+
74
+				case 'parents':
75
+					$exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"';
76
+					$exp .= ' && (obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"';
77
+					$parent = $sitetree->getParent();
78
+					while ($parent && $parent->exists()) {
79
+						$exp .= ' || obj.http.x-url ~ "^'.preg_quote($parent->Link()).'$"';
80
+						$parent = $parent->getParent();
81
+					}
82
+					$exp .= ')';
83
+					break;
84
+
85
+				case 'all':
86
+					$exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"';
87
+					break;
88
+
89
+				case 'everyting':
90
+				default:
91
+					$exp = 'obj.http.x-url ~ /';
92
+					break;
93
+
94
+			}
95
+
96
+			return static::performFlush($exp);
97
+		}
98
+
99
+		return false;
100
+	}
101 101
     
102
-    public static function flushURL($url) {
103
-        if ($url) {
104
-            $exp = 'obj.http.x-url ~ "^'.preg_quote($url).'$"';
105
-            return static::performFlush($exp);
106
-        }
107
-        return false;
108
-    }
109
-
110
-    protected static function performFlush($banExpression)
111
-    {
112
-        $success = true;
113
-        $urls = static::getUrls();
114
-        // config loaded successfully
102
+	public static function flushURL($url) {
103
+		if ($url) {
104
+			$exp = 'obj.http.x-url ~ "^'.preg_quote($url).'$"';
105
+			return static::performFlush($exp);
106
+		}
107
+		return false;
108
+	}
109
+
110
+	protected static function performFlush($banExpression)
111
+	{
112
+		$success = true;
113
+		$urls = static::getUrls();
114
+		// config loaded successfully
115 115
 		if (static::checkConfig()) {
116 116
 			if (count($urls) > 0) {
117 117
 				foreach ($urls as $url) {
@@ -140,126 +140,126 @@  discard block
 block discarded – undo
140 140
 			}
141 141
 		}
142 142
 		
143
-        return $success;
144
-    }
145
-
146
-    protected static function getService($url, $banExpression)
147
-    {
148
-        // prepare API call
149
-        $service = new RestfulService(
150
-            $url,
151
-            0 // expiry time 0: do not cache the API call
152
-        );
153
-        // set basic auth
154
-        $username = Config::inst()->get('SectionIO', 'username');
155
-        $password = Config::inst()->get('SectionIO', 'password');
156
-        $service->basicAuth($username, $password);
157
-        // set query string (ban expression)
158
-        $service->setQueryString(array(
159
-            'banExpression' => $banExpression,
160
-        ));
161
-
162
-        return $service;
163
-    }
164
-
165
-    protected static function getOptions()
166
-    {
167
-        // prepare curl options for ssl verification
168
-        if (Config::inst()->get('SectionIO', 'verify_ssl')) {
169
-            return array(
170
-                CURLOPT_SSL_VERIFYPEER => 0,
171
-                CURLOPT_SSL_VERIFYHOST => 0,
172
-            );
173
-        }
174
-        return array();
175
-    }
176
-
177
-    protected static function getHeaders()
178
-    {
179
-        $headers = array(
180
-            'Content-Type: application/json',
181
-            'Accept: application/json',
182
-        );
183
-
184
-        return $headers;
185
-    }
186
-
187
-    protected static function getUrls()
188
-    {
189
-        $urls = array();
190
-
191
-        if (static::checkConfig()) {
192
-            $api_url = Config::inst()->get('SectionIO', 'api_url');
193
-            $account_id = Config::inst()->get('SectionIO', 'account_id');
194
-            $application_id = Config::inst()->get('SectionIO', 'application_id');
195
-            $application_ids = array();
196
-            if (is_string($application_id)) {
197
-                $application_ids = preg_split("/[\s,]+/", $application_id);
198
-            } elseif (is_array($application_id)) {
199
-                $application_ids = $application_id;
200
-            }
201
-            $environment_name = Config::inst()->get('SectionIO', 'environment_name');
202
-            $proxy_name = Config::inst()->get('SectionIO', 'proxy_name');
203
-
204
-            foreach ($application_ids as $appid) {
205
-                // build API URL: /account/{accountId}/application/{applicationId}/environment/{environmentName}/proxy/{proxyName}/state
206
-                $urls[] = Controller::join_links(
207
-                    $api_url,
208
-                    'account',
209
-                    $account_id,
210
-                    'application',
211
-                    $appid,
212
-                    'environment',
213
-                    $environment_name,
214
-                    'proxy',
215
-                    $proxy_name,
216
-                    'state'
217
-                );
218
-            }
219
-        }
220
-
221
-        return $urls;
222
-    }
223
-
224
-    protected static function checkConfig()
225
-    {
226
-        $missing = array();
227
-        // check config
228
-        $api_url = Config::inst()->get('SectionIO', 'api_url');
229
-        if (!isset($api_url) || strlen($api_url) < 1) {
230
-            $missing[] = 'SectionIO.api_url';
231
-        }
232
-        $account_id = Config::inst()->get('SectionIO', 'account_id');
233
-        if (!isset($account_id) || strlen($account_id) < 1) {
234
-            $missing[] = 'SectionIO.account_id';
235
-        }
236
-        $application_id = Config::inst()->get('SectionIO', 'application_id');
237
-        if (!isset($application_id) || (!is_array($application_id) && strlen((string) $application_id) < 1)) {
238
-            $missing[] = 'SectionIO.application_id';
239
-        }
240
-        $environment_name = Config::inst()->get('SectionIO', 'environment_name');
241
-        if (!isset($environment_name) || strlen($environment_name) < 1) {
242
-            $missing[] = 'SectionIO.environment_name';
243
-        }
244
-        $proxy_name = Config::inst()->get('SectionIO', 'proxy_name');
245
-        if (!isset($proxy_name) || strlen($proxy_name) < 1) {
246
-            $missing[] = 'SectionIO.proxy_name';
247
-        }
248
-        $username = Config::inst()->get('SectionIO', 'username');
249
-        if (!isset($username) || strlen($username) < 1) {
250
-            $missing[] = 'SectionIO.username';
251
-        }
252
-        $password = Config::inst()->get('SectionIO', 'password');
253
-        if (!isset($password) || strlen($password) < 1) {
254
-            $missing[] = 'SectionIO.password';
255
-        }
143
+		return $success;
144
+	}
145
+
146
+	protected static function getService($url, $banExpression)
147
+	{
148
+		// prepare API call
149
+		$service = new RestfulService(
150
+			$url,
151
+			0 // expiry time 0: do not cache the API call
152
+		);
153
+		// set basic auth
154
+		$username = Config::inst()->get('SectionIO', 'username');
155
+		$password = Config::inst()->get('SectionIO', 'password');
156
+		$service->basicAuth($username, $password);
157
+		// set query string (ban expression)
158
+		$service->setQueryString(array(
159
+			'banExpression' => $banExpression,
160
+		));
161
+
162
+		return $service;
163
+	}
164
+
165
+	protected static function getOptions()
166
+	{
167
+		// prepare curl options for ssl verification
168
+		if (Config::inst()->get('SectionIO', 'verify_ssl')) {
169
+			return array(
170
+				CURLOPT_SSL_VERIFYPEER => 0,
171
+				CURLOPT_SSL_VERIFYHOST => 0,
172
+			);
173
+		}
174
+		return array();
175
+	}
176
+
177
+	protected static function getHeaders()
178
+	{
179
+		$headers = array(
180
+			'Content-Type: application/json',
181
+			'Accept: application/json',
182
+		);
183
+
184
+		return $headers;
185
+	}
186
+
187
+	protected static function getUrls()
188
+	{
189
+		$urls = array();
190
+
191
+		if (static::checkConfig()) {
192
+			$api_url = Config::inst()->get('SectionIO', 'api_url');
193
+			$account_id = Config::inst()->get('SectionIO', 'account_id');
194
+			$application_id = Config::inst()->get('SectionIO', 'application_id');
195
+			$application_ids = array();
196
+			if (is_string($application_id)) {
197
+				$application_ids = preg_split("/[\s,]+/", $application_id);
198
+			} elseif (is_array($application_id)) {
199
+				$application_ids = $application_id;
200
+			}
201
+			$environment_name = Config::inst()->get('SectionIO', 'environment_name');
202
+			$proxy_name = Config::inst()->get('SectionIO', 'proxy_name');
203
+
204
+			foreach ($application_ids as $appid) {
205
+				// build API URL: /account/{accountId}/application/{applicationId}/environment/{environmentName}/proxy/{proxyName}/state
206
+				$urls[] = Controller::join_links(
207
+					$api_url,
208
+					'account',
209
+					$account_id,
210
+					'application',
211
+					$appid,
212
+					'environment',
213
+					$environment_name,
214
+					'proxy',
215
+					$proxy_name,
216
+					'state'
217
+				);
218
+			}
219
+		}
220
+
221
+		return $urls;
222
+	}
223
+
224
+	protected static function checkConfig()
225
+	{
226
+		$missing = array();
227
+		// check config
228
+		$api_url = Config::inst()->get('SectionIO', 'api_url');
229
+		if (!isset($api_url) || strlen($api_url) < 1) {
230
+			$missing[] = 'SectionIO.api_url';
231
+		}
232
+		$account_id = Config::inst()->get('SectionIO', 'account_id');
233
+		if (!isset($account_id) || strlen($account_id) < 1) {
234
+			$missing[] = 'SectionIO.account_id';
235
+		}
236
+		$application_id = Config::inst()->get('SectionIO', 'application_id');
237
+		if (!isset($application_id) || (!is_array($application_id) && strlen((string) $application_id) < 1)) {
238
+			$missing[] = 'SectionIO.application_id';
239
+		}
240
+		$environment_name = Config::inst()->get('SectionIO', 'environment_name');
241
+		if (!isset($environment_name) || strlen($environment_name) < 1) {
242
+			$missing[] = 'SectionIO.environment_name';
243
+		}
244
+		$proxy_name = Config::inst()->get('SectionIO', 'proxy_name');
245
+		if (!isset($proxy_name) || strlen($proxy_name) < 1) {
246
+			$missing[] = 'SectionIO.proxy_name';
247
+		}
248
+		$username = Config::inst()->get('SectionIO', 'username');
249
+		if (!isset($username) || strlen($username) < 1) {
250
+			$missing[] = 'SectionIO.username';
251
+		}
252
+		$password = Config::inst()->get('SectionIO', 'password');
253
+		if (!isset($password) || strlen($password) < 1) {
254
+			$missing[] = 'SectionIO.password';
255
+		}
256 256
         
257
-        if (count($missing) > 0) {
257
+		if (count($missing) > 0) {
258 258
 			if (!Director::isDev()) {
259 259
 				SS_Log::log('SectionIO:: config parameters missing: ' . implode(', ', $missing), SS_Log::WARN);
260 260
 			}
261
-            return false;
262
-        }
263
-        return true;
264
-    }
261
+			return false;
262
+		}
263
+		return true;
264
+	}
265 265
 }
Please login to merge, or discard this patch.