Skip to content

Commit

Permalink
Changed Controller for a behavior
Browse files Browse the repository at this point in the history
This way you don't have to extend Controller anymore, just use a
behavior. Much tidier and simpler that way.
  • Loading branch information
Patroklo committed Sep 20, 2015
1 parent d713dc7 commit 1cf130a
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 58 deletions.
50 changes: 50 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

.idea/deployment.xml
114 changes: 114 additions & 0 deletions BladeBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace cyneek\yii2\blade;


use yii\base\Behavior;
use yii\web\View;
use Yii;

class BladeBehavior extends Behavior
{
/**
* @inheritdoc
*/
public function init()
{
// Will add an event before rendering the view file.
Yii::$app->getView()->on(View::EVENT_BEFORE_RENDER, [$this, 'viewRender']);
}

/**
*
*
* @param \yii\base\ViewEvent $event
* @return mixed
* @throws \yii\base\InvalidConfigException
*/
function viewRender($event)
{

/** @var View $view */
$view = $event->sender;

/** @var \yii\web\Controller $controller */
$controller = $this->owner;

/** @var String $viewFile */
$viewFile = $event->viewFile;

/**
* The layout file defined for that controller
*
* @var String $layoutFile
*/
$layoutFile = $controller->findLayoutFile($view);

// If layout is not a string, won't be necessary to do anything more.
if (!is_string($layoutFile))
{
return;
}

/** @var String $layoutExt */
$layoutExt = pathinfo($layoutFile, PATHINFO_EXTENSION);

/** @var String $viewExt */
$viewExt = pathinfo($viewFile, PATHINFO_EXTENSION);

/**
* The defined blade extension file for blade Renderer
* @var String $bladeExtension
*/
$bladeExtension = $this->getRendererExtension($view);

// If the two files (view and layout) are blade files, chances are
// that developer has used a blade-like layout code, so we will
// have to launch first the layout and after that the view, while
// Yii2 does that the other way round.
if ($layoutExt === $bladeExtension && $viewExt === $bladeExtension)
{
$view->renderers[$bladeExtension]->addLayout($layoutFile);

// we don't want to use the layout anymore.
$controller->layout = FALSE;
}

}


/**
* Checks for the defined Blade file extension
* for the renderer. Since we don't know the name
* the developer has given to the renderer we will have
* to check it one by one.
*
*
* @param View $view
* @return string
* @throws \yii\base\InvalidConfigException
*/
function getRendererExtension($view)
{
$renderList = $view->renderers;

$extension = '';

foreach ($renderList as $key => $renderer)
{

if (is_array($renderer) && trim($renderer['class'], '\\') == trim(ViewRenderer::className(), '\\'))
{
$view->renderers[$key] = Yii::createObject($view->renderers[$key]);
$extension = $view->renderers[$key]->extension;
}
elseif (is_object($renderer) && get_class($renderer) == ViewRenderer::className())
{
$extension = $view->renderers[$key]->extension;
}
}

return $extension;
}

}
45 changes: 0 additions & 45 deletions Controller.php

This file was deleted.

32 changes: 19 additions & 13 deletions ViewRenderer.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created for Yii2.
* User: Joseba Juániz [email protected]
* Date: 16/09/15
* Time: 12:23
*/

namespace cyneek\yii2\blade;

Expand All @@ -22,7 +16,7 @@ class ViewRenderer extends BaseViewRenderer
*
* @var string
*/
public static $extension = 'blade';
public $extension = 'blade';
/**
* Path where view cache will be located
* Warning: this path must be writable by PHP.
Expand Down Expand Up @@ -71,7 +65,7 @@ public function init()

$this->blade = new Blade($this->viewPaths, Yii::getAlias($this->cachePath));

$this->blade->view()->addExtension(self::$extension, self::$extension);
$this->blade->view()->addExtension($this->extension, $this->extension);
}


Expand All @@ -86,8 +80,7 @@ public function init()
public function render($view, $viewFile, $params)
{

$viewFile = pathinfo($viewFile, PATHINFO_FILENAME);

$viewFile = $this->normalizeView($viewFile);

if (is_null($this->base_view))
{
Expand All @@ -109,7 +102,7 @@ public function render($view, $viewFile, $params)
/**
* Returns View blade Object
*
* @return \Philo\Blade\Illuminate\View\Factory|void
* @return \Philo\Blade\Illuminate\View\Factory
*/
public function view()
{
Expand All @@ -124,8 +117,21 @@ public function view()
*/
public function addLayout($layout)
{
$layout = pathinfo($layout, PATHINFO_FILENAME);
$this->base_view = $layout;
$this->base_view = $this->normalizeView($layout);
}

/**
* @param String $view
* @return mixed
*/
protected function normalizeView($view)
{
$directory = pathinfo($view, PATHINFO_DIRNAME);
$viewFile = pathinfo($view, PATHINFO_FILENAME);

$this->blade->view()->getFinder()->addLocation($directory.'/');

return $viewFile;
}

}

0 comments on commit 1cf130a

Please sign in to comment.