Skip to content

Commit

Permalink
Environment Label is ready for Craft 3!
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrog committed Dec 24, 2017
0 parents commit f8a3f32
Show file tree
Hide file tree
Showing 14 changed files with 710 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,11 @@
# Environment Label Changelog

_A plugin for Craft CMS 3.x to help distinguish your Craft environments ...so you don't forget where you are._

The format of this file is based on [Keep a Changelog](http://keepachangelog.com/). This project adheres to [Semantic Versioning](http://semver.org/).

## 3.0.0 - 2017-12-25

### Added

- Environment Label is ready for Craft 3!
9 changes: 9 additions & 0 deletions LICENSE.md
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2017 Michael Rog

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
138 changes: 138 additions & 0 deletions README.md
@@ -0,0 +1,138 @@
# Environment Label

_...so you don't forget where you are._

**A [Top Shelf Craft](https://topshelfcraft.com) creation**
in collaboration with [Kind](https://madebykind.com/)

— Based on the original [LabelEnvironment](https://github.com/madebykind/craft.labelenvironment) plugin by [Tom Davies](https://github.com/tomdavies)


### TL;DR.

The _Environment Label_ plugin adds a nice coloured banner to your CraftCMS control panel so you'll never forget what environment you're using.

The colors and text of the environment label are configurable via the plugin config file.

![Screenshot](docs/dev.jpg)

* * *


## Installation

Visit the _Plugin Store_ in your Craft control panel, search for **Environment Label**, and click to _Install_ the plugin.


## Configuration

By default, the environment label will pull in the value of Craft's `CRAFT_ENVIRONMENT` constant, which is set to the current hostname unless you override it.

_(In other words, out of the box, you get a red banner with white text that alerts you to the current hostname.)_

You can use a plugin config file to tweak the appearance and text of the environment label for each installation.

Simply add an `environment-label.php` file to your `config` directory.

(There is a sample plugin config included in the plugin files - `config/environment-label.php` - which you can copy and use as a starter.

```php
<?php

return [

'showLabel' => true,
'labelText' => CRAFT_ENVIRONMENT,
'prefix' => null,
'suffix' => null,
'labelColor' => '#cc5643',
'textColor' => '#ffffff',

];
```

I suggest referencing [PHP environment variables](http://php.net/manual/en/function.getenv.php), rather than using hard-coded values, to make your configuration more consistently maintainable.

I also _highly recommend_ using the [PHP dot-env](https://github.com/vlucas/phpdotenv) package to easily set and deploy environment variables across your installations. (The [Craft starter project](https://github.com/craftcms/craft)) ships with dot-env included.)

For example, your `environment-label.php` config file might use environment variables set by the server:

```php
<?php

return [

'showLabel' => getenv('CRAFT_ENV_SHOW_LABEL'),
'labelText' => getenv('CRAFT_ENV_LABEL_TEXT'),

);
```

For added flexibility, the full text of the label will be rendered as a Twig template, so you can also include template variables if you want:

```php
<?php

return [

'suffixText' => " // {{ currentUser }}",

);
```


## Changing Settings in the Control Panel

You can also make basic changes to the text and appearance of the environment label via the plugin Settings page.

![Settings](docs/settings.jpg)

</div>

This is provided as a convenience for easily testing out the plugin, but for full customizability, you should use a plugin config file as described above.

(Settings specified in the plugin config file will override any changes made via the Settings page in the control panel.)


## Twig template globals

_Environment Label_ makes its properties available via a Twig template global variable, so you can create your own
environment label rendering in your public templates:

```twig
{{ environmentLabel.renderedText }}
{{ environmentLabel.labelColor }}
{{ environmentLabel.textColor }}
```

## JavaScript globals

_Environment Label_ also makes its properties available as JS globals on each authenticated CP page.

```js
window.CRAFT_ENVIRONMENT
window.CRAFT_ENVIRONMENT_LABEL
```

## What are the system requirements?

Craft 3.0+ and PHP 7.0+


## I've found a bug.

No you haven't.


## Yes, I believe I have.

Well, alright. Please open a [GitHub Issue](https://github.com/topshelfcraft/Environment-Label/issues), and if you're feeling ambitious, submit a PR to the `dev` branch.


* * *

### Contributors:

- Plugin development: [Michael Rog](http://michaelrog.com) / @michaelrog
- Craft 2 plugin development: [Tom Davies](https://github.com/tomdavies) / @metadaptive
- Icon: [NAS](http://nasztu.com/), via [The Noun Project](https://thenounproject.com/search/?q=label&i=28588)
44 changes: 44 additions & 0 deletions composer.json
@@ -0,0 +1,44 @@
{
"name": "topshelfcraft/environment-label",
"description": "...so you don't forget where you are.",
"type": "craft-plugin",
"version": "3.0.0",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"environment label"
],
"support": {
"docs": "https://raw.githubusercontent.com/TopShelfCraft/Environment-Label",
"issues": "https://github.com/TopShelfCraft/Environment-Label/issues"
},
"license": "MIT",
"authors": [
{
"name": "Top Shelf Craft (Michael Rog)",
"homepage": "https://topshelfcraft.com"
}
],
"require": {
"craftcms/cms": "^3.0.0-RC3"
},
"autoload": {
"psr-4": {
"topshelfcraft\\environmentlabel\\": "src/"
}
},
"extra": {
"name": "Environment Label",
"handle": "environment-label",
"schemaVersion": "1.0.0",
"hasCpSettings": true,
"hasCpSection": false,
"changelogUrl": "https://raw.githubusercontent.com/TopShelfCraft/Environment-Label/master/CHANGELOG.md",
"components": {
"label": "topshelfcraft\\environmentlabel\\services\\Label"
},
"class": "topshelfcraft\\environmentlabel\\EnvironmentLabel"
}
}
31 changes: 31 additions & 0 deletions config/environment-label.php
@@ -0,0 +1,31 @@
<?php

/**
* Environment Label configuration
*
* This file is provided as a template.
* If you want to customize these plugin settings for your project,
* copy this `environment-label.php` file into your `config` directory,
* and un-comment the values you want to customize.
*
* I suggest using PHP environment variables, rather than hard-coded values,
* to make your configuration more consistently maintainable.
* (c.f. http://php.net/manual/en/function.getenv.php)
*
* I also suggest using the PHP dot-env package to make it easy to set and maintain
* environment variables across your installations. (The default Craft project ships with dot-env included.)
* (c.f. https://github.com/vlucas/phpdotenv)
*
* Some code examples are provided here for your reference...
*/

return [

// 'showLabel' => getenv('ENVIRONMENT_SHOW_LABEL'),
// 'labelText' => CRAFT_ENVIRONMENT,
// 'prefixText' => '',
// 'suffixText' => '',
// 'labelColor' => getenv('ENVIRONMENT_LABEL_COLOR'),
// 'textColor' => '#ffffff',

];
Binary file added docs/dev.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/settings.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions src/EnvironmentLabel.php
@@ -0,0 +1,101 @@
<?php
/**
* Environment Label
*
* @author Michael Rog <michael@michaelrog.com>, Tom Davies <tom@madebykind.com>
* @link https://topshelfcraft.com
* @copyright Copyright 2017, Top Shelf Craft (Michael Rog)
* @see https://github.com/topshelfcraft/Environment-Label
*/

namespace topshelfcraft\environmentlabel;

use Craft;
use craft\base\Plugin;
use topshelfcraft\environmentlabel\models\Settings;
use topshelfcraft\environmentlabel\services\Label;
use topshelfcraft\environmentlabel\twigextensions\EnvironmentLabelTwigExtension;


/**
* @author Michael Rog <michael@michaelrog.com>
* @package EnvironmentLabel
* @since 3.0.0
*
* @property Label $label
* @property Settings $settings
*
* @method Settings getSettings()
*/
class EnvironmentLabel extends Plugin
{


/*
* Static properties
*/

/**
* @var EnvironmentLabel
*/
public static $plugin;


/*
* Public methods
*/

/**
* Initializes the plugin, sets its static self-reference, registers the Twig extension,
* and adds the environment label as appropriate.
*/
public function init()
{

parent::init();
self::$plugin = $this;

Craft::$app->view->registerTwigExtension(new EnvironmentLabelTwigExtension());

EnvironmentLabel::$plugin->label->doItBaby();

}


/*
* Protected methods
*/


/**
* Creates and returns the model used to store the plugin’s settings.
*
* @return \topshelfcraft\environmentlabel\models\Settings|null
*/
protected function createSettingsModel()
{
return new Settings();
}


/**
* Returns the rendered settings HTML, which will be inserted into the content
* block on the settings page.
*
* @return string The rendered settings HTML
*
* @throws \Twig_Error_Loader
* @throws \yii\base\Exception
*/
protected function settingsHtml(): string
{
return Craft::$app->view->renderTemplate(
'environment-label/settings',
[
'settings' => $this->getSettings()
]
);
}


}
29 changes: 29 additions & 0 deletions src/icon.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f8a3f32

Please sign in to comment.