From 6b31cb5360db1fbaa933a248bb6e7e9d776eafac Mon Sep 17 00:00:00 2001 From: Mats Mikkel Rummelhoff Date: Mon, 11 Oct 2021 14:01:12 +0200 Subject: [PATCH] WIP --- src/Bunny.php | 25 +++++++++++++++ src/config.php | 6 ++-- src/helpers/BunnyHelper.php | 45 ++++++++++++++++++++++++++ src/icon.svg | 1 + src/models/PullZone.php | 64 +++++++++++++++++++++++++++++++++++++ src/models/Settings.php | 12 +++++-- src/web/twig/Extension.php | 26 +++++++++++++++ 7 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 src/helpers/BunnyHelper.php create mode 100644 src/icon.svg create mode 100644 src/models/PullZone.php create mode 100644 src/web/twig/Extension.php diff --git a/src/Bunny.php b/src/Bunny.php index d0c7e52..58d26da 100644 --- a/src/Bunny.php +++ b/src/Bunny.php @@ -4,6 +4,12 @@ use Craft; use craft\base\Plugin; +use craft\services\Plugins; + +use vaersaagod\bunny\models\Settings; +use vaersaagod\bunny\web\twig\Extension; + +use yii\base\Event; class Bunny extends Plugin { @@ -28,6 +34,9 @@ public function init() { parent::init(); + // Add in our Twig extensions + Craft::$app->getView()->registerTwigExtension(new Extension()); + Craft::info( Craft::t( 'bunny', @@ -38,4 +47,20 @@ public function init() ); } + /** + * @return Settings + */ + public function getSettings(): Settings + { + return parent::getSettings(); + } + + /** + * @return Settings + */ + protected function createSettingsModel(): Settings + { + return new Settings(); + } + } diff --git a/src/config.php b/src/config.php index bc94f22..c19222c 100644 --- a/src/config.php +++ b/src/config.php @@ -1,10 +1,12 @@ [ + 'pullingEnabled' => true, + 'pullZones' => [ 'default' => [ - 'hostname' => 'https://yourpullzone.b-cdn.net', + 'hostname' => '',//https://yourpullzone.b-cdn.net', 'enabled' => true, ], ], + 'defaultPullZone' => 'default', ]; diff --git a/src/helpers/BunnyHelper.php b/src/helpers/BunnyHelper.php new file mode 100644 index 0000000..fb74bf2 --- /dev/null +++ b/src/helpers/BunnyHelper.php @@ -0,0 +1,45 @@ +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); + } + +} diff --git a/src/icon.svg b/src/icon.svg new file mode 100644 index 0000000..521a18d --- /dev/null +++ b/src/icon.svg @@ -0,0 +1 @@ + diff --git a/src/models/PullZone.php b/src/models/PullZone.php new file mode 100644 index 0000000..2302d91 --- /dev/null +++ b/src/models/PullZone.php @@ -0,0 +1,64 @@ +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); + } + +} diff --git a/src/models/Settings.php b/src/models/Settings.php index c902e23..6da91f8 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -1,13 +1,19 @@