Created
February 27, 2018 14:22
-
-
Save sanchezzzhak/f258e0d53ca9f9e200474ed35747bfbd to your computer and use it in GitHub Desktop.
Yii2 theme component snipet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\components; | |
use Yii; | |
use yii\base\InvalidConfigException; | |
use yii\helpers\ArrayHelper; | |
use yii\helpers\FileHelper; | |
class Theme extends \yii\base\Theme | |
{ | |
public $active; | |
/** | |
* @inheritdoc | |
*/ | |
public function applyTo($path) | |
{ | |
$pathMap = ArrayHelper::getValue($this->pathMap,$this->active,$this->pathMap); | |
if (empty($pathMap)) { | |
if (($basePath = $this->getBasePath()) === null) { | |
throw new InvalidConfigException('The "basePath" property must be set.'); | |
} | |
$pathMap = [Yii::$app->getBasePath() => [$basePath]]; | |
} | |
$path = FileHelper::normalizePath($path); | |
foreach ($pathMap as $from => $tos) { | |
$from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR; | |
if (strpos($path, $from) === 0) { | |
$n = strlen($from); | |
foreach ((array) $tos as $to) { | |
$to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR; | |
$file = $to . substr($path, $n); | |
if (is_file($file)) { | |
return $file; | |
} | |
} | |
} | |
} | |
return $path; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// configuration components add section | |
'view' => [ | |
'theme' => [ | |
'class' => 'app\components\Theme', | |
'active' => 'theme-juddy', | |
'pathMap' => [ | |
'theme-user' => [ | |
'@app/views' => ['@app/themes/theme-user/views'] | |
], | |
'theme-dashboard' => [ | |
'@app/views' => ['@app/themes/theme-dashboard/views'] | |
] | |
] | |
], | |
], | |
], | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment