Skip to content

Commit

Permalink
yii\base\Module createControllerByID add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
easydowork committed Feb 11, 2024
1 parent 0027227 commit 2ecfff3
Showing 1 changed file with 32 additions and 18 deletions.
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 createController($route)
*/
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->get('cache')){
$cacheKey = [Yii::$app->id,Yii::getAlias('@app'),__FUNCTION__,$id];
$className = Yii::$app->cache->get($cacheKey);
}

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 = '';
}else{
if(Yii::$app->get('cache')) {
Yii::$app->cache->set($cacheKey, $className);
}
}
}

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

0 comments on commit 2ecfff3

Please sign in to comment.