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

Adds posibility to change domain after changes in ngrok.io about doma… #9

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
16 changes: 16 additions & 0 deletions Api/Data/DomainInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Shkoliar\Ngrok\Api\Data;

interface DomainInterface
{
public const NGROK_IO = '.ngrok.io';

public const NGROK_APP = '.ngrok.app';

public const NGROK_DEV = '.ngrok.dev';

public const NGROK_FREE_APP = '.ngrok-free.app';

public const NGROK_FREE_DEV = '.ngrok-free.dev';
}
25 changes: 21 additions & 4 deletions Helper/Ngrok.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@

namespace Shkoliar\Ngrok\Helper;

class Ngrok extends \Magento\Framework\App\Helper\AbstractHelper
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Shkoliar\Ngrok\Model\Config;

class Ngrok extends AbstractHelper
{
const SCHEME_HTTP = 'http';
const SCHEME_HTTPS = 'https';

const NGROK_DOMAIN = '.ngrok.io';

const HTTP_X_FORWARDED_PROTO = 'HTTP_X_FORWARDED_PROTO';

/**
* @param Context $context
* @param Config $config
*/
public function __construct(
protected Context $context,
private Config $config
) {
parent::__construct($context);
}

/**
* Get server parameter value by key
*
Expand All @@ -38,7 +51,11 @@ public function getDomain()
{
$ngrokDomain = $this->getServer('HTTP_X_ORIGINAL_HOST') ?: $this->getServer('HTTP_HOST');

return stripos($ngrokDomain, self::NGROK_DOMAIN) !== false ? $ngrokDomain : false;
$configuredNgrokDomain = $this->config->isCustomDomainEnabled() ?
$this->config->getCustomDomain() :
$this->config->getDomain();

return stripos($ngrokDomain, $configuredNgrokDomain) !== false ? $ngrokDomain : false;
}

/**
Expand Down
49 changes: 49 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Shkoliar\Ngrok\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

class Config
{
private const XML_IS_CUSTOM_DOMAIN = 'ngrok/general/enable_custom_domain';
private const XML_DOMAIN = 'ngrok/general/domain';
private const XML_CUSTOM_DOMAIN = 'ngrok/general/custom_domain';

/**
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
private ScopeConfigInterface $scopeConfig
) {}

/**
* @return bool
*/
public function isCustomDomainEnabled(): bool
{
return $this->scopeConfig->isSetFlag(
self::XML_IS_CUSTOM_DOMAIN
);
}

/**
* @return string
*/
public function getDomain(): string
{
return (string)$this->scopeConfig->getValue(
self::XML_DOMAIN
);
}

/**
* @return string
*/
public function getCustomDomain(): string
{
return (string)$this->scopeConfig->getValue(
self::XML_CUSTOM_DOMAIN
);
}
}
33 changes: 33 additions & 0 deletions Model/Config/Source/Domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Shkoliar\Ngrok\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;
use Shkoliar\Ngrok\Api\Data\DomainInterface;

class Domain implements OptionSourceInterface
{
/**
* @return array
*/
public function toOptionArray(): array
{
return [
[
'label' => DomainInterface::NGROK_IO, 'value' => DomainInterface::NGROK_IO
],
[
'label' => DomainInterface::NGROK_APP, 'value' => DomainInterface::NGROK_APP
],
[
'label' => DomainInterface::NGROK_DEV, 'value' => DomainInterface::NGROK_DEV
],
[
'label' => DomainInterface::NGROK_FREE_APP, 'value' => DomainInterface::NGROK_FREE_APP
],
[
'label' => DomainInterface::NGROK_FREE_DEV, 'value' => DomainInterface::NGROK_FREE_DEV
]
];
}
}
11 changes: 11 additions & 0 deletions etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin" title="Magento Admin">
<resource id="Shkoliar_Ngrok::config" title="Shkoliar Ngrok"/>
</resource>
</resources>
</acl>
</config>
52 changes: 52 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="ngrok_tab" translate="label" sortOrder="1000">
<label>Ngrok</label>
</tab>
<section id="ngrok"
translate="label"
sortOrder="10"
showInDefault="1">
<class>separator-top</class>
<label>Ngrok</label>
<tab>ngrok_tab</tab>
<resource>Shkoliar_Ngrok::config</resource>
<group id="general"
translate="label"
type="text"
sortOrder="10"
showInDefault="1">
<label>General</label>
<field id="domain"
translate="label"
type="select"
sortOrder="10"
showInDefault="1">
<label>Domain</label>
<validate>required-entry</validate>
<source_model>Shkoliar\Ngrok\Model\Config\Source\Domain</source_model>
</field>
<field id="enable_custom_domain"
translate="label"
type="select"
sortOrder="20"
showInDefault="1">
<validate>required-entry</validate>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="custom_domain"
translate="label"
type="text"
sortOrder="30"
showInDefault="1">
<label>Custom Domain</label>
<depends>
<field id="ngrok/general/enable_custom_domain">1</field>
</depends>
</field>
</group>
</section>
</system>
</config>
12 changes: 12 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<ngrok>
<general>
<enable_custom_domain>0</enable_custom_domain>
<domain>.ngrok-free.app</domain>
</general>
</ngrok>
</default>
</config>
7 changes: 6 additions & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Shkoliar_Ngrok" setup_version="1.0.0" />
<module name="Shkoliar_Ngrok">
<sequence>
<module name="Magento_Config"/>
<module name="Magento_Store"/>
</sequence>
</module>
</config>