Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Maintenance module extension now provides CWP proxy information for HTTP requests #127

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions _config/maintenance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
Name: maintenanceproxyextensions
Before: updatecheckerextensions
Only:
moduleexists: silverstripe-maintenance
---
BringYourOwnIdeas\Maintenance\Util\ComposerLoader:
extensions:
- CWP\CWP\Extension\MaintenanceProxyExtension

BringYourOwnIdeas\Maintenance\Util\SupportedAddonsLoader:
extensions:
- CWP\CWP\Extension\MaintenanceProxyExtension
58 changes: 58 additions & 0 deletions code/extensions/MaintenanceProxyExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace CWP\CWP\Extension;

use Extension;

/**
* Used to configure proxy settings for bringyourownideas/silverstripe-maintenance and its related modules
*
* @see https://www.cwp.govt.nz/developer-docs/en/2/how_tos/external_http_requests_with_proxy
*/
class MaintenanceProxyExtension extends Extension
{
/**
* Configures required environment settings for Composer's use, applies to
* {@link \BringYourOwnIdeas\Maintenance\Util\ComposerLoader} and is applied before ComposerLoaderExtension in
* bringyourownideas/silverstripe-composer-update-checker to ensure the proxy information is set before Composer
* is created
*/
public function onAfterBuild()
{
// Provide access for Composer's StreamContextFactory, since it creates its own stream context
if ($proxy = $this->getCwpProxy()) {
$_SERVER['CGI_HTTP_PROXY'] = $proxy;
}
}

/**
* Provide proxy options for {@link \BringYourOwnIdeas\Maintenance\Util\ApiLoader} instances to use in
* their Guzzle clients
*
* @param array $options
*/
public function updateClientOptions(&$options)
{
if ($proxy = $this->getCwpProxy()) {
$options['proxy'] = $proxy;
}
}

/**
* Returns a formatted CWP proxy string, e.g. `tcp://proxy.cwp.govt.nz:1234`
*
* @return string
*/
protected function getCwpProxy()
{
if (!defined('SS_OUTBOUND_PROXY') || !defined('SS_OUTBOUND_PROXY_PORT')) {
return '';
}

return sprintf(
'tcp://%s:%d',
SS_OUTBOUND_PROXY,
SS_OUTBOUND_PROXY_PORT
);
}
}