Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow env config for attributes #13666

Draft
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 26 additions & 20 deletions src/helpers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
use craft\web\User as WebUser;
use craft\web\View;
use HTMLPurifier_Encoder;
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionProperty;
use yii\base\Event;
use yii\base\Exception;
use yii\base\InvalidArgumentException;
use yii\base\InvalidValueException;
use yii\base\Model;
use yii\helpers\Inflector;
use yii\mutex\FileMutex;
use yii\web\JsonParser;
Expand Down Expand Up @@ -134,34 +136,38 @@ public static function env(string $name): mixed
* For example, if an object has a `fooBar` property, and `X`/`X_` is passed as the prefix, the resulting array
* may contain a `fooBar` key set to an `X_FOO_BAR` environment variable value, if it exists.
*
* @param string $class The class name
* @param object|string $class The class name or object
* @phpstan-param class-string $class
* @param string|null $envPrefix The environment variable name prefix
* @return array
* @phpstan-return array<string, mixed>
* @since 4.0.0
*/
public static function envConfig(string $class, ?string $envPrefix = null): array
public static function envConfig(object|string $class, ?string $envPrefix = null): array
{
$envPrefix = $envPrefix !== null ? StringHelper::ensureRight($envPrefix, '_') : '';
$properties = (new ReflectionClass($class))->getProperties(ReflectionProperty::IS_PUBLIC);
$envConfig = [];

foreach ($properties as $prop) {
if ($prop->isStatic()) {
continue;
}

$propName = $prop->getName();
$envName = $envPrefix . strtoupper(StringHelper::toSnakeCase($propName));
$envValue = static::env($envName);

if ($envValue !== null) {
$envConfig[$propName] = $envValue;
}
}

return $envConfig;
$isModel = (new ReflectionClass($class))->isSubclassOf(Model::class);

/** @var ?Model $model */
$model = $isModel
? ($class instanceof Model ? $class : Craft::createObject($class))
: null;

$properties = $model
? Collection::make($model->attributes())
: Collection::make((new ReflectionClass($class))->getProperties(ReflectionProperty::IS_PUBLIC))
->filter(fn(ReflectionProperty $prop) => !$prop->isStatic())
->map(fn(ReflectionProperty $prop) => $prop->getName());

return $properties
->mapWithKeys(function(string $propName) use ($envPrefix) {
$envName = $envPrefix . strtoupper(StringHelper::toSnakeCase($propName));
$envValue = static::env($envName);

return [$propName => $envValue];
})
->whereNotNull()
->all();
}

/**
Expand Down