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

yii\base\Module createControllerByID add cache #20115

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions framework/base/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,28 +641,42 @@
*/
public function createControllerByID($id)
{
$pos = strrpos($id, '/');
if ($pos === false) {
$prefix = '';
$className = $id;
} else {
$prefix = substr($id, 0, $pos + 1);
$className = substr($id, $pos + 1);
}
if(Yii::$app->has('cache')){
$cacheKey = [Yii::$app->id,Yii::getAlias('@app'),__FUNCTION__,$id];
$className = Yii::$app->cache->get($cacheKey);

Check warning on line 646 in framework/base/Module.php

View check run for this annotation

Codecov / codecov/patch

framework/base/Module.php#L645-L646

Added lines #L645 - L646 were not covered by tests
}

if(empty($className)){
$pos = strrpos($id, '/');
if ($pos === false) {
$prefix = '';
$className = $id;
} else {
$prefix = substr($id, 0, $pos + 1);
$className = substr($id, $pos + 1);
}

if ($this->isIncorrectClassNameOrPrefix($className, $prefix)) {
return null;
}
if ($this->isIncorrectClassNameOrPrefix($className, $prefix)) {
return null;
}

$className = preg_replace_callback('%-([a-z0-9_])%i', function ($matches) {
return ucfirst($matches[1]);
}, ucfirst($className)) . 'Controller';
$className = ltrim($this->controllerNamespace . '\\' . str_replace('/', '\\', $prefix) . $className, '\\');
if (strpos($className, '-') !== false || !class_exists($className)) {
return null;
$className = preg_replace_callback('%-([a-z0-9_])%i', function ($matches) {
return ucfirst($matches[1]);
}, ucfirst($className)) . 'Controller';
$className = ltrim($this->controllerNamespace . '\\' . str_replace('/', '\\', $prefix) . $className, '\\');
if (strpos($className, '-') !== false || !class_exists($className)) {
return null;
}
if (!is_subclass_of($className, 'yii\base\Controller')) {
$className = '';

Check warning on line 671 in framework/base/Module.php

View check run for this annotation

Codecov / codecov/patch

framework/base/Module.php#L671

Added line #L671 was not covered by tests
}else{
if(Yii::$app->has('cache')) {
Yii::$app->cache->set($cacheKey, $className);

Check warning on line 674 in framework/base/Module.php

View check run for this annotation

Codecov / codecov/patch

framework/base/Module.php#L674

Added line #L674 was not covered by tests
}
}
}

if (is_subclass_of($className, 'yii\base\Controller')) {
if (!empty($className)) {
$controller = Yii::createObject($className, [$id, $this]);
return get_class($controller) === $className ? $controller : null;
} elseif (YII_DEBUG) {
Expand Down
Loading