-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
17 changed files
with
239 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\behaviors; | ||
|
||
use Craft; | ||
use craft\base\FieldInterface; | ||
use craft\helpers\StringHelper; | ||
use craft\models\FieldLayout; | ||
use yii\base\Behavior; | ||
use yii\base\InvalidConfigException; | ||
use yii\base\Model; | ||
use yii\validators\UrlValidator; | ||
|
||
/** | ||
* EnvAttributeParserBehavior can be applied to models with attributes that can be | ||
* set to either environment variables (`$VARIABLE_NAME`) or aliases (`@aliasName`)`. | ||
* | ||
* --- | ||
* | ||
* ```php | ||
* public function attributes() | ||
* { | ||
* return [ | ||
* 'parser' => [ | ||
* 'class' => EnvAttributeParserBehavior::class, | ||
* 'attributes' => ['attr1', 'attr2', '...'], | ||
* ], | ||
* ]; | ||
* } | ||
* ``` | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 3.1 | ||
*/ | ||
class EnvAttributeParserBehavior extends Behavior | ||
{ | ||
/** | ||
* @var Model | ||
*/ | ||
public $owner; | ||
|
||
/** | ||
* @var string[] The attributes names that can be set to environment | ||
* variables (`$VARIABLE_NAME`) and/or aliases (`@aliasName`). | ||
*/ | ||
public $attributes = []; | ||
|
||
/** | ||
* @var array Keeps track of the original attribute values | ||
*/ | ||
private $_values; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function events() | ||
{ | ||
return [ | ||
Model::EVENT_BEFORE_VALIDATE => 'beforeValidate', | ||
Model::EVENT_AFTER_VALIDATE => 'afterValidate', | ||
]; | ||
} | ||
|
||
/** | ||
* Replaces attribute values before validation occurs. | ||
*/ | ||
public function beforeValidate() | ||
{ | ||
$this->_values = []; | ||
|
||
foreach ($this->attributes as $attribute) { | ||
$value = $this->owner->$attribute; | ||
if (($parsed = Craft::parseEnv($value)) !== $value) { | ||
$this->_values[$attribute] = $value; | ||
$this->owner->$attribute = $parsed; | ||
|
||
foreach ($this->owner->getActiveValidators($attribute) as $validator) { | ||
if ($validator instanceof UrlValidator) { | ||
$validator->defaultScheme = null; | ||
} | ||
|
||
$validator->message = StringHelper::ensureRight($validator->message, ' ({value})'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Restores the original attribute values after validation occurs. | ||
*/ | ||
public function afterValidate() | ||
{ | ||
foreach ($this->_values as $attribute => $value) { | ||
$this->owner->$attribute = $value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.