Skip to content

Instantly share code, notes, and snippets.

@sanchezzzhak
Created February 27, 2018 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanchezzzhak/f258e0d53ca9f9e200474ed35747bfbd to your computer and use it in GitHub Desktop.
Save sanchezzzhak/f258e0d53ca9f9e200474ed35747bfbd to your computer and use it in GitHub Desktop.
Yii2 theme component snipet
<?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;
}
}
<?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