Skip to content

Commit dd3bb53

Browse files
committed
view overriding in application
1 parent ca81f9b commit dd3bb53

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/Module.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use yii\base\BootstrapInterface;
77
use yii\base\Module as BaseModule;
88
use yii\console\Application as ConsoleApplication;
9+
use yii\web\Application as WebApplication;
910

1011
/**
1112
* @author Dmytro Karpovych
@@ -28,11 +29,15 @@ public static function getRootDir()
2829
*/
2930
public function bootstrap($app)
3031
{
32+
if ($app instanceof WebApplication) {
33+
$app->setComponents([
34+
'view' => ['class' => 'nullref\core\components\View'],
35+
]);
36+
}
3137
if ($app instanceof ConsoleApplication) {
3238
$app->controllerMap['module'] = [
3339
'class' => 'nullref\core\console\ModuleController',
3440
];
3541
}
3642
}
37-
3843
}

src/components/View.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* @author Dmytro Karpovych
4+
* @copyright 2015 NRE
5+
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
6+
*/
7+
8+
namespace nullref\core\components;
9+
10+
use Yii;
11+
use yii\base\InvalidCallException;
12+
use yii\base\ViewContextInterface;
13+
use yii\web\View as BaseView;
14+
15+
class View extends BaseView
16+
{
17+
protected function findViewFile($view, $context = null)
18+
{
19+
if (strncmp($view, '@', 1) === 0) {
20+
// e.g. "@app/views/main"
21+
$file = Yii::getAlias($view);
22+
} elseif (strncmp($view, '//', 2) === 0) {
23+
// e.g. "//layouts/main"
24+
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
25+
} elseif (strncmp($view, '/', 1) === 0) {
26+
// e.g. "/site/index"
27+
if (Yii::$app->controller !== null) {
28+
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
29+
} else {
30+
throw new InvalidCallException("Unable to locate view file for view '$view': no active controller.");
31+
}
32+
} elseif ($context instanceof ViewContextInterface) {
33+
$file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
34+
if (Yii::$app->controller !== null) {
35+
$newFile = Yii::getAlias('@app/modules/'
36+
. Yii::$app->controller->module->uniqueId
37+
. '/views/' . Yii::$app->controller->id
38+
. '/' . $view . '.' . $this->defaultExtension);
39+
if (file_exists($newFile)) {
40+
$file = $newFile;
41+
}
42+
}
43+
} elseif (($currentViewFile = $this->getViewFile()) !== false) {
44+
$file = dirname($currentViewFile) . DIRECTORY_SEPARATOR . $view;
45+
} else {
46+
throw new InvalidCallException("Unable to resolve view file for view '$view': no active view context.");
47+
}
48+
49+
if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
50+
return $file;
51+
}
52+
$path = $file . '.' . $this->defaultExtension;
53+
if ($this->defaultExtension !== 'php' && !is_file($path)) {
54+
$path = $file . '.php';
55+
}
56+
57+
return $path;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)