Skip to content

Commit 315f5f8

Browse files
committed
Merge branch '4.17' of https://github.com/craftcms/cms into 5.9
# Conflicts: # CHANGELOG-WIP.md
2 parents 094882c + 3536086 commit 315f5f8

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGELOG-WIP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
- Added support for referencing environment variables anywhere within settings that support them (e.g. `foo/$ENV_NAME/bar` or `foo-${ENV_NAME}-bar`). ([#17794](https://github.com/craftcms/cms/pull/17794))
5757
- Environmental settings can now reference `CRAFT_SITE` (the current site’s handle) and `CRAFT_SITE_UPPER` (the current site’s handle in UPPER_SNAKE_CASE) environment variables, which are defined at runtime. ([#17794](https://github.com/craftcms/cms/pull/17794))
5858
- It’s now possible to create unpublished drafts via GraphQL. ([#17805](https://github.com/craftcms/cms/pull/17805))
59+
- It’s no longer possible to instantiate objects that don’t extend `yii\base\BaseObject` via the `create()` Twig function. (GHSA-94rc-cqvm-m4pw)
5960
- Added the `randomString()` Twig function. ([#18020](https://github.com/craftcms/cms/discussions/18020))
6061
- Added the `uuid()` Twig function.
6162
- The Twig `hash` filter now supports passing a hashing algorithm, such as `'md5'` or `'sha256'`. ([#17885](https://github.com/craftcms/cms/issues/17885))

src/Craft.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ public static function getAlias($alias, $throwException = true)
5656
/**
5757
* @inheritdoc
5858
* @template T
59-
* @param class-string<T>|array|callable $type
60-
* @phpstan-param class-string<T>|array{class:class-string<T>}|callable():T $type
59+
* @param class-string<T>|array{class:class-string<T>}|array{__class:class-string<T>}|callable():T $type
6160
* @param array $params
6261
* @return T
6362
*/

src/web/twig/Extension.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
use Twig\TwigFilter;
8282
use Twig\TwigFunction;
8383
use Twig\TwigTest;
84+
use yii\base\BaseObject;
8485
use yii\base\InvalidArgumentException;
8586
use yii\base\InvalidConfigException;
8687
use yii\db\Exception;
@@ -1390,7 +1391,7 @@ public function getFunctions(): array
13901391
new TwigFunction('combine', 'array_combine'),
13911392
new TwigFunction('configure', [Craft::class, 'configure']),
13921393
new TwigFunction('cpUrl', [UrlHelper::class, 'cpUrl']),
1393-
new TwigFunction('create', [Craft::class, 'createObject']),
1394+
new TwigFunction('create', [$this, 'createFunction']),
13941395
new TwigFunction('dataUrl', [$this, 'dataUrlFunction']),
13951396
new TwigFunction('date', [$this, 'dateFunction'], ['needs_environment' => true]),
13961397
new TwigFunction('dump', [$this, 'dumpFunction'], ['is_safe' => ['html'], 'needs_context' => true, 'is_variadic' => true]),
@@ -1472,6 +1473,26 @@ public function collectFunction(mixed $var): Collection
14721473
return $collection;
14731474
}
14741475

1476+
/**
1477+
* Creates a new object.
1478+
*
1479+
* @template T of BaseObject
1480+
* @param class-string<T>|array{class:class-string<T>}|array{__class:class-string<T>} $type
1481+
* @param array $params
1482+
* @return T
1483+
* @since 5.9.0
1484+
*/
1485+
public function createFunction(string|array $type, array $params = []): BaseObject
1486+
{
1487+
$class = is_string($type) ? $type : ($type['__class'] ?? $type['class'] ?? null);
1488+
if (!is_subclass_of($class, BaseObject::class)) {
1489+
throw new InvalidArgumentException(sprintf('create() can only be used to create instances of %s.', BaseObject::class));
1490+
}
1491+
1492+
/** @var BaseObject */
1493+
return Craft::createObject($type, $params);
1494+
}
1495+
14751496
/**
14761497
* Generates a base64-encoded [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) for the given file path or asset.
14771498
*

0 commit comments

Comments
 (0)