Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maderlock committed May 11, 2018
0 parents commit 26f341a
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 0 deletions.
27 changes: 27 additions & 0 deletions C3EnvironmentBanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace C3EnvironmentBanner;

use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\InstallContext;

if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}

/**
* Class ShyimProfiler
* @package ShyimProfiler
*/
class C3EnvironmentBanner extends Plugin
{
/**
* @param ActivateContext $context
*/
public function activate(ActivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_DEFAULT);
}

}
16 changes: 16 additions & 0 deletions Resources/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- Subscriber -->
<service id="c3_environment_banner.subscriber.post_dispatch_secure_backend_index"
class="C3EnvironmentBanner\Subscriber\Backend">
<argument>%c3_environment_banner.plugin_dir%</argument>
<argument type="service" id="service_container"/>
<tag name="shopware.event_subscriber"/>
</service>
</services>
</container>
31 changes: 31 additions & 0 deletions Resources/views/backend/c3EnvironmentBanner/index/header.tpl

Large diffs are not rendered by default.

146 changes: 146 additions & 0 deletions Subscriber/Backend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

namespace C3EnvironmentBanner\Subscriber;

use Enlight\Event\SubscriberInterface;
use Shopware\Components\DependencyInjection\Container;

class Backend implements SubscriberInterface
{
/**
* @var Container
*/
protected $container;

/**
* @var string
*/
protected $pluginPath;

/**
* @todo Make a configuration that can be adjusted as required
* @var array
*/
protected $colorMap = [
'dev' => '0,255,0',
'staging' => '255,127,0',
'preview' => '255,0,255',
'production' => '255,0,0'
];

/**
* Backend constructor.
*
* @param string $pluginPath
* @param Container $container
*/
public function __construct(
$pluginPath,
Container $container
)
{
$this->pluginPath = $pluginPath;
$this->container = $container;
}

public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Action_PostDispatchSecure_Backend_Index'
=> 'onPostDispatchSecureBackendIndex'
];
}

public function onPostDispatchSecureBackendIndex(\Enlight_Event_EventArgs $args)
{
// Get environment specific values
$environment = getenv('SHOPWARE_ENV');
if (!isset($this->colorMap[$environment])) {
return;
}
$primaryColour = $this->colorMap[$environment];

// Add backend template
$this->container->get('Template')->addTemplateDir(
$this->getPath() . '/Resources/views/'
);
$view = $args->getSubject()->View();
$view->extendsTemplate('backend/c3EnvironmentBanner/index/header.tpl');

// Set environment-specific values for colour and label
$view->assign('environment', ucfirst($environment));
$colVar = $colorVariants = $this->getColorVariants($primaryColour);
$view->assign('rgbBackGrad1', $colVar['lighter1']);
$view->assign('rgbBackGrad2', $colVar['lighter2']);
$view->assign('rgbBackGrad3', $colVar['lighter3']);
$view->assign('rgbBackGrad4', $colVar['lighter4']);
$view->assign('rgbBorderTop', $colVar['mid']);
$view->assign('rgbBorderBottom', $colVar['midDarker']);
}

/**
* Calculate variations of primary colour as strings
*
* @param string $primaryColor
*
* @return array
*/
protected function getColorVariants($primaryColor)
{
$col = explode(',', $primaryColor);

// Minimum and maximum values for RGB 0-255 for lightening effects
$lighterCoEfficients = [
[171, 238],
[218, 245],
[223, 246],
[198, 246],
];

// Calculate lighter versions of colours based on co-efficients
$lighter = [];
for ($i=0; $i<count($lighterCoEfficients); $i++) {
$lighter[$i] = $col;
$min = $lighterCoEfficients[$i][0];
$max = $lighterCoEfficients[$i][1];
foreach ($lighter[$i] as &$c) {
$c = ($c / 255) * ($max-$min);
$c += $min;
}
}

// Calculate darker colours by contrast and brightening
$darkeningAdjustments = [
[0.4, 60],
[0.2, 60]
];
$mid = $col;
foreach ($mid as &$c) {
$contrastCoefficient = $darkeningAdjustments[0][0];
$brightnessAdjust = $darkeningAdjustments[0][1];
$c *= $contrastCoefficient;
$c += $brightnessAdjust;
}
$midDarker = $col;
foreach ($midDarker as &$c) {
$contrastCoefficient = $darkeningAdjustments[1][0];
$brightnessAdjust = $darkeningAdjustments[1][1];
$c *= $contrastCoefficient;
$c += $brightnessAdjust;
}

return [
'mid' => implode(',',$mid),
'midDarker' => implode(',',$midDarker),
'lighter1' => implode(',',$lighter[0]),
'lighter2' => implode(',',$lighter[1]),
'lighter3' => implode(',',$lighter[2]),
'lighter4' => implode(',',$lighter[3])
];
}

protected function getPath()
{
return $this->pluginPath;
}
}
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "c3/shopware-environment-banner",
"keywords": ["shopware", "development", "plugin"],
"description": "Environment backend styling plugin for Shopware to avoid accidental live changes",
"license": "MIT",
"type": "shopware-plugin",
"extra": {
"installer-name": "C3EnvironmentBanner"
},
"require": {
"composer/installers": "~1.0"
}
}
Binary file added plugin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../engine/Shopware/Components/Plugin/schema/plugin.xsd">

<label lang="de">Environment Banner</label>
<label lang="en">Environment Banner</label>

<version>1.0.0</version>
<copyright>(c) by C3</copyright>
<license>MIT</license>
<link>https://github.com/c3limited/C3ShopwareEnvironmentBanner</link>
<author>C3</author>
<compatibility minVersion="5.4.0" />

<description>
<![CDATA[
<b>How to use</b>
<p>Once installed and active, the SHOPWARE_ENV environment variable will be used to determine what colour to display in the backend. Values recognised are: dev, staging, preview and production. Other values are ignored.</p>
]]>
</description>

<changelog version="1.0.0">
<changes lang="de">Erstveröffentlichung - Farbe für das Backend einstellen</changes>
<changes lang="en">First release - color for backend</changes>
</changelog>

</plugin>
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
C3 Shopware Environment Banner
==============================

Once installed and active, the SHOPWARE_ENV environment variable will be used to determine what colour to display in the backend. Values recognised are: dev, staging, preview and production. Other values are ignored.

Installation
------------

Copy the contents of this repo to a new folder C3EnvironmentBanner in custom/plugins/ and install and activate as normal via the admin panel or command line.

## Installing via composer

To install via composer, add this repo to the repository area of composer.json and run the following command:

`composer require c3/shopware-environment-banner`

Then install and activate as normal.

0 comments on commit 26f341a

Please sign in to comment.