This repository has been archived by the owner on Apr 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
<?php | ||
|
||
return [ | ||
'pullzones' => [ | ||
'pullingEnabled' => true, | ||
'pullZones' => [ | ||
'default' => [ | ||
'hostname' => 'https://yourpullzone.b-cdn.net', | ||
'hostname' => '',//https://yourpullzone.b-cdn.net', | ||
'enabled' => true, | ||
], | ||
], | ||
'defaultPullZone' => 'default', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace vaersaagod\bunny\helpers; | ||
|
||
use Craft; | ||
use craft\helpers\StringHelper; | ||
use craft\helpers\UrlHelper; | ||
|
||
use vaersaagod\bunny\Bunny; | ||
use vaersaagod\bunny\models\PullZone; | ||
|
||
use yii\base\InvalidConfigException; | ||
|
||
class BunnyHelper | ||
{ | ||
|
||
/** | ||
* @param string $path | ||
* @param string|null $pullzone | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
public static function bunnyPullUrl(string $path = '', ?string $pullzone = null): string | ||
{ | ||
$pullZone = static::getPullZone($pullzone); | ||
return $pullZone->getUrl($path); | ||
} | ||
|
||
/** | ||
* @param string|null $key | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
public static function getPullZone(?string $key = null): PullZone | ||
{ | ||
$settings = Bunny::getInstance()->getSettings(); | ||
$key = $key ?? $settings->defaultPullZone; | ||
$pullZoneConfig = $settings->pullZones[$key] ?? null; | ||
if (!$pullZoneConfig) { | ||
throw new InvalidConfigException("Invalid pull zone \"$key\""); | ||
} | ||
return new PullZone($pullZoneConfig); | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace vaersaagod\bunny\models; | ||
|
||
use Craft; | ||
use craft\base\Model; | ||
use craft\helpers\StringHelper; | ||
use craft\helpers\UrlHelper; | ||
|
||
use vaersaagod\bunny\Bunny; | ||
|
||
class PullZone extends Model | ||
{ | ||
|
||
/** @var bool */ | ||
public $enabled = true; | ||
|
||
/** @var string */ | ||
public $hostname; | ||
|
||
/** | ||
* @param array $config | ||
* @throws \craft\errors\SiteNotFoundException | ||
*/ | ||
public function __construct($config = []) | ||
{ | ||
parent::__construct($config); | ||
if (!UrlHelper::isFullUrl($this->hostname)) { | ||
$this->hostname = StringHelper::ensureLeft($this->hostname, 'https://'); | ||
} else { | ||
$this->hostname = UrlHelper::urlWithScheme($this->hostname, 'https'); | ||
} | ||
$this->hostname = StringHelper::removeRight($this->hostname, '/'); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return string | ||
*/ | ||
public function getUrl(string $path = ''): string | ||
{ | ||
$baseSiteUrl = Craft::getAlias('@web'); | ||
$url = StringHelper::removeLeft($path, $baseSiteUrl); | ||
if ( | ||
!$this->enabled | ||
|| !Bunny::getInstance()->getSettings()->pullingEnabled | ||
|| UrlHelper::isAbsoluteUrl($url) | ||
|| UrlHelper::isProtocolRelativeUrl($url) | ||
) { | ||
if (!$url) { | ||
return ''; | ||
} | ||
if (UrlHelper::isFullUrl($url)) { | ||
return $url; | ||
} | ||
return UrlHelper::url($url); | ||
} | ||
if ($url) { | ||
$url = StringHelper::ensureLeft($url, '/'); | ||
} | ||
return UrlHelper::url($this->hostname . $url); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
<?php | ||
|
||
namespace vaersaagod\models; | ||
namespace vaersaagod\bunny\models; | ||
|
||
use craft\base\Model; | ||
|
||
class Settings extends Model | ||
{ | ||
|
||
/** @var array */ | ||
public $pullzones = []; | ||
/** @var bool */ | ||
public $pullingEnabled = true; | ||
|
||
/** @var array[] */ | ||
public $pullZones = []; | ||
|
||
/** @var string */ | ||
public $defaultPullZone; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace vaersaagod\bunny\web\twig; | ||
|
||
use Craft; | ||
|
||
use Twig\Extension\AbstractExtension; | ||
use Twig\Extension\GlobalsInterface; | ||
use Twig\TwigFunction; | ||
|
||
use vaersaagod\bunny\helpers\BunnyHelper; | ||
|
||
class Extension extends AbstractExtension | ||
{ | ||
|
||
/** | ||
* @return TwigFunction[] | ||
*/ | ||
public function getFunctions() | ||
{ | ||
return [ | ||
new TwigFunction('bunnyPullUrl', [BunnyHelper::class, 'bunnyPullUrl']), | ||
]; | ||
} | ||
|
||
} |