Skip to content
Permalink
Browse files

Make cell creation a protected method.

This will let classes using CellTrait to more easily the cell creation
process.
  • Loading branch information...
markstory committed Aug 25, 2014
1 parent 4261c5b commit 348e5ce43cf3d731ad792041ff40a0710a73b9fd
Showing with 30 additions and 18 deletions.
  1. +30 −18 src/View/CellTrait.php
@@ -65,35 +65,21 @@ public function cell($cell, array $data = [], array $options = []) {
}
list($plugin, $cellName) = pluginSplit($pluginAndCell);
$className = App::className($pluginAndCell, 'View/Cell', 'Cell');
if (!$className) {
throw new Error\MissingCellException(array('className' => $pluginAndCell . 'Cell'));
}
$cellInstance = new $className($this->request, $this->response, $this->eventManager(), $options);
$cellInstance->template = Inflector::underscore($action);
$cellInstance->plugin = !empty($plugin) ? $plugin : null;
$cellInstance->theme = !empty($this->theme) ? $this->theme : null;
if (!empty($this->helpers)) {
$cellInstance->helpers = $this->helpers;
}
if (isset($this->viewClass)) {
$cellInstance->viewClass = $this->viewClass;
}
if ($this instanceof View) {
$cellInstance->viewClass = get_class($this);
}
$cell = $this->_createCell($className, $action, $plugin, $options);
if (!empty($data)) {
$data = array_values($data);
}
try {
$reflect = new \ReflectionMethod($cellInstance, $action);
$reflect->invokeArgs($cellInstance, $data);
return $cellInstance;
$reflect = new \ReflectionMethod($cell, $action);
$reflect->invokeArgs($cell, $data);
return $cell;
} catch (\ReflectionException $e) {
throw new \BadMethodCallException(sprintf(
'Class %s does not have a "%s" method.',
@@ -103,4 +89,30 @@ public function cell($cell, array $data = [], array $options = []) {
}
}
/**
* Create and configure the cell instance.
*
* @param string $className The cell classname.
* @param string $action The action name.
* @param string $plugin The plugin name.
* @param array $options The constructor options for the cell.
* @return Cake\View\Cell;
*/
protected function _createCell($className, $action, $plugin, $options) {
$instance = new $className($this->request, $this->response, $this->eventManager(), $options);
$instance->template = Inflector::underscore($action);
$instance->plugin = !empty($plugin) ? $plugin : null;
$instance->theme = !empty($this->theme) ? $this->theme : null;
if (!empty($this->helpers)) {
$instance->helpers = $this->helpers;
}
if (isset($this->viewClass)) {
$instance->viewClass = $this->viewClass;
}
if ($this instanceof View) {
$instance->viewClass = get_class($this);
}
return $instance;
}
}

0 comments on commit 348e5ce

Please sign in to comment.
You can’t perform that action at this time.