|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
namespace CWP\CWP\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use BringYourOwnIdeas\Maintenance\Reports\SiteSummary; |
|
|
|
|
|
|
6
|
|
|
use SilverStripe\Core\Environment; |
|
7
|
|
|
use SilverStripe\Core\Extension; |
|
8
|
|
|
|
|
9
|
|
|
if (!class_exists(SiteSummary::class)) { |
|
10
|
|
|
return; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Used to configure proxy settings for bringyourownideas/silverstripe-maintenance and its related modules |
|
15
|
|
|
* |
|
16
|
|
|
* @see https://www.cwp.govt.nz/developer-docs/en/2/how_tos/external_http_requests_with_proxy |
|
17
|
|
|
*/ |
|
18
|
|
|
class MaintenanceProxyExtension extends Extension |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Configures required environment settings for Composer's use, applies to |
|
22
|
|
|
* {@link \BringYourOwnIdeas\Maintenance\Util\ComposerLoader} and is applied before ComposerLoaderExtension in |
|
23
|
|
|
* bringyourownideas/silverstripe-composer-update-checker to ensure the proxy information is set before Composer |
|
24
|
|
|
* is created |
|
25
|
|
|
*/ |
|
26
|
|
|
public function onAfterBuild() |
|
27
|
|
|
{ |
|
28
|
|
|
// Mock COMPOSER_HOME if it's not defined already. Composer requires one of the two to be set. |
|
29
|
|
|
if (!Environment::getEnv('HOME') && !Environment::getEnv('COMPOSER_HOME')) { |
|
30
|
|
|
putenv('COMPOSER_HOME=/tmp'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
// Provide access for Composer's StreamContextFactory, since it creates its own stream context |
|
34
|
|
|
if ($proxy = $this->getCwpProxy()) { |
|
35
|
|
|
$_SERVER['CGI_HTTP_PROXY'] = $proxy; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Provide proxy options for {@link \BringYourOwnIdeas\Maintenance\Util\ApiLoader} instances to use in |
|
41
|
|
|
* their Guzzle clients |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
*/ |
|
45
|
|
|
public function updateClientOptions(&$options) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($proxy = $this->getCwpProxy()) { |
|
48
|
|
|
$options['proxy'] = $proxy; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns a formatted CWP proxy string, e.g. `tcp://proxy.cwp.govt.nz:1234` |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getCwpProxy() |
|
58
|
|
|
{ |
|
59
|
|
|
if (!Environment::getEnv('SS_OUTBOUND_PROXY') || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT')) { |
|
60
|
|
|
return ''; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return sprintf( |
|
64
|
|
|
'tcp://%s:%d', |
|
65
|
|
|
Environment::getEnv('SS_OUTBOUND_PROXY'), |
|
66
|
|
|
Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.