Skip to content

Instantly share code, notes, and snippets.

@ryanscherler
Created November 6, 2020 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanscherler/d30ccb5a7c23f28dfca1dea26bd43c37 to your computer and use it in GitHub Desktop.
Save ryanscherler/d30ccb5a7c23f28dfca1dea26bd43c37 to your computer and use it in GitHub Desktop.
Yii Module for Craft CMS to enable local volumes for development when using Digital Ocean Spaces as an asset volume.
<?php
namespace modules;
use yii\base\Module;
use Craft;
use craft\volumes\Local;
use vaersaagod\dospaces\Volume;
/**
* Custom module class.
*
* This class will be available throughout the system via:
* `Craft::$app->getModule('local-volumes-module')`.
*
* Add the following to your modules array in config/app.php
*
* 'modules' => [
* //...
* 'do-spaces-local-volumes-module' => \modules\DOSpacesLocalVolumesModule::class,
* ];
*
* If you want the module to get loaded on every request,
* add the following to your bootstrap array in config/app.php:
*
* 'bootstrap' => [
* //...
* 'do-spaces-local-volumes-module',
* ];
*
* Learn more about Yii module development in Yii's documentation:
* http://www.yiiframework.com/doc-2.0/guide-structure-modules.html
*/
class DOSpacesLocalVolumesModule extends Module
{
/**
* Initializes the module.
*/
public function init()
{
parent::init();
// If this is the dev environment, use Local volumes instead of Digital Ocean Spaces Volume
if (Craft::$app->env === 'dev') {
Craft::$container->set(Volume::class, function($container, $params, $config) {
if (empty($config['id'])) {
return new Volume($config);
}
return new Local([
'id' => $config['id'],
'uid' => $config['uid'],
'name' => $config['name'],
'handle' => $config['handle'],
'hasUrls' => $config['hasUrls'],
'url' => "@web/local-volumes/{$config['handle']}",
'path' => "@webroot/local-volumes/{$config['handle']}",
'sortOrder' => $config['sortOrder'],
'dateCreated' => $config['dateCreated'],
'dateUpdated' => $config['dateUpdated'],
'fieldLayoutId' => $config['fieldLayoutId'],
]);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment