-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Widget issue in layout page. Changed the syntax of Widget. Widg…
…et can call inside view page, for modules, and routing. return Widget::make('admin:user', function ($widget) { $widget->isModule(true); return $widget->render(); }, array('msg' => 'Hello! Widget', 'userId' => $id)); or echo Widget::make('admin:user');
- Loading branch information
1 parent
20bf30c
commit 51fc671
Showing
2 changed files
with
191 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,190 @@ | ||
<?php | ||
namespace Cygnite\Mvc\View; | ||
|
||
use Cygnite\Proxy\StaticResolver; | ||
|
||
if (!defined('CF_SYSTEM')) { | ||
exit('External script access not allowed'); | ||
} | ||
|
||
class Widget extends StaticResolver implements \ArrayAccess | ||
class Widget implements \ArrayAccess | ||
{ | ||
public $widget = array(); | ||
|
||
protected function make($name, $arguments = array()) | ||
public $data = array(); | ||
|
||
protected $module = false; | ||
|
||
protected $widgetName; | ||
|
||
/** | ||
* @param $name | ||
* @param array $data | ||
*/ | ||
public function __construct($name, $data= array()) | ||
{ | ||
$this->widgetName = $name; | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* @param $var | ||
* @param callable $callback | ||
* @param array $data | ||
* @return mixed | ||
*/ | ||
public static function make($var, \Closure $callback = null, $data = array()) | ||
{ | ||
if ($this->has($name)) { | ||
return $this->widget[$name]; | ||
/* | ||
| If second param given as closure then we will | ||
| return callback | ||
*/ | ||
if ($callback instanceof \Closure && !is_null($callback)) { | ||
return $callback(new Widget($var, $data)); | ||
} | ||
|
||
if (strpos($name, '::') != false) { | ||
$expression = explode('::', $name); | ||
/* | ||
| return object | ||
*/ | ||
return (new Widget($var, $data))->render(); | ||
} | ||
|
||
/** | ||
* @param $bool | ||
*/ | ||
public function isModule($bool) | ||
{ | ||
$this->module = $bool; | ||
} | ||
|
||
/** | ||
* @param bool $isModule | ||
* @return null | ||
*/ | ||
public function render($isModule = false) | ||
{ | ||
/* | ||
| In some case you may not want to write much code | ||
| in such case you have option to pass param into render | ||
| so that we will understand you are trying to invoke module view | ||
*/ | ||
if ($isModule) { | ||
$this->isModule($isModule); | ||
} | ||
|
||
//we will check is modules is available in given string | ||
// if not we will look for view widget into the normal views directory | ||
if (strpos($name, 'modules') !== false) { | ||
$views = $expression[0]; | ||
$moduleViews = DS.'Views'.DS; | ||
/* | ||
| We will check if widget is cached, return if already cached | ||
*/ | ||
if ($this->has($this->widgetName)) { | ||
return $this->getWidget($this->widgetName); | ||
} | ||
|
||
$path = null; | ||
|
||
if ($this->module) { | ||
/* | ||
| If widget belongs to HMVC modules and | ||
| has ":" in the view name, we will think first param | ||
| as module name and second param as view name | ||
*/ | ||
if (string_has($this->widgetName, ':')) { | ||
|
||
$exp = array(); | ||
$exp = explode(':', $this->widgetName); | ||
$moduleName = $exp[0]; | ||
$view = $exp[1]; | ||
$path = getcwd().DS.APPPATH.DS.'modules'.DS.$moduleName.DS.'Views'.DS.$view.'.view'.EXT; | ||
|
||
$this->widgetName= null; | ||
$this->module = false; | ||
} | ||
|
||
|
||
} else { | ||
$moduleViews = ''; | ||
$views = 'views'.DS.$expression[0]; | ||
|
||
/* | ||
| If widget not belongs to HMVC modules and | ||
| has ":" in the view name, we will convert name as path | ||
*/ | ||
if (string_has($this->widgetName, ':')) { | ||
$widget = null; | ||
$widget = str_replace(':', DS, $this->widgetName); | ||
$path = getcwd().DS.APPPATH.DS.'views'.DS.$widget.'.view'.EXT; | ||
} | ||
} | ||
|
||
$path = getcwd().DS.APPPATH.DS.$views.DS.$expression[1].$moduleViews; | ||
$output = (new Output($this->widgetName))->buffer($path, $this->data)->clean(); | ||
$this->setWidget($this->widgetName, $output); | ||
|
||
$v = isset($expression[2]) && string_has($expression[2], '.') ? str_replace('.', DS, $expression[2]) : ''; | ||
return $this->getWidget($this->widgetName); | ||
} | ||
|
||
$view = $path.$v.'.view'.EXT; | ||
public function __toString() | ||
{ | ||
return $this->getWidget($this->widgetName); | ||
} | ||
|
||
Output::load($view, $arguments); | ||
$output = Output::endBuffer(); | ||
/** | ||
* @param $name | ||
* @param $value | ||
*/ | ||
public function setWidget($name, $value) | ||
{ | ||
$this->widget[$name] = $value; | ||
} | ||
|
||
return $this[$name] = $output; | ||
/** | ||
* @param $name | ||
* @return null | ||
*/ | ||
public function getWidget($name) | ||
{ | ||
return isset($this->widget[$name]) ? $this->widget[$name] : null; | ||
} | ||
|
||
protected function has($key) | ||
/** | ||
* @param $key | ||
* @return bool | ||
*/ | ||
public function has($key) | ||
{ | ||
return isset($this->data[$key]) ? true :false; | ||
return isset($this->widget[$key]) ? true :false; | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Whether a offset exists | ||
* | ||
* @link http://php.net/manual/en/arrayaccess.offsetexists.php | ||
* @param mixed $offset <p> | ||
* An offset to check for. | ||
* </p> | ||
* @return boolean true on success or false on failure. | ||
* </p> | ||
* <p> | ||
* The return value will be casted to boolean if non-boolean was returned. | ||
* ArrayAccess | ||
* @param int|string $offset | ||
* @return bool | ||
*/ | ||
public function offsetExists($offset) | ||
{ | ||
// TODO: Implement offsetExists() method. | ||
return isset($this->data[$offset]); | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Offset to retrieve | ||
* @link http://php.net/manual/en/arrayaccess.offsetget.php | ||
* @param mixed $offset <p> | ||
* The offset to retrieve. | ||
* </p> | ||
* @return mixed Can return all value types. | ||
* ArrayAccess | ||
* @param int|string $offset | ||
* @return mixed | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
// TODO: Implement offsetGet() method. | ||
return $this->offsetExists($offset) ? $this->data[$offset] : null; | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Offset to set | ||
* @link http://php.net/manual/en/arrayaccess.offsetset.php | ||
* @param mixed $offset <p> | ||
* The offset to assign the value to. | ||
* </p> | ||
* @param mixed $value <p> | ||
* The value to set. | ||
* </p> | ||
* @return void | ||
* ArrayAccess | ||
* @param int|string $offset | ||
* @param mixed $value | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
// TODO: Implement offsetSet() method. | ||
$this->data[$offset] = $value; | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Offset to unset | ||
* @link http://php.net/manual/en/arrayaccess.offsetunset.php | ||
* @param mixed $offset <p> | ||
* The offset to unset. | ||
* </p> | ||
* @return void | ||
* ArrayAccess | ||
* @param int|string $offset | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
// TODO: Implement offsetUnset() method. | ||
}} | ||
unset($this->data[$offset]); | ||
} | ||
} |