Skip to content
This repository has been archived by the owner on Apr 6, 2024. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed Oct 11, 2021
1 parent 3b83a67 commit 6b31cb5
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/Bunny.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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',
Expand All @@ -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();
}

}
6 changes: 4 additions & 2 deletions src/config.php
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',
];
45 changes: 45 additions & 0 deletions src/helpers/BunnyHelper.php
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);
}

}
1 change: 1 addition & 0 deletions src/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions src/models/PullZone.php
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);
}

}
12 changes: 9 additions & 3 deletions src/models/Settings.php
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;

}
26 changes: 26 additions & 0 deletions src/web/twig/Extension.php
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']),
];
}

}

0 comments on commit 6b31cb5

Please sign in to comment.