Skip to content

Commit

Permalink
Fixed Widget issue in layout page. Changed the syntax of Widget. Widg…
Browse files Browse the repository at this point in the history
…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
sanjoydesk committed Apr 7, 2015
1 parent 20bf30c commit 51fc671
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 74 deletions.
61 changes: 50 additions & 11 deletions src/Cygnite/Mvc/View/Output.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
<?php
namespace Cygnite\Mvc\View;

use Cygnite\Proxy\StaticResolver;

if (!defined('CF_SYSTEM')) {
exit('External script access not allowed');
}

class Output extends StaticResolver
class Output
{
private $output = array();

private $name;

/*protected function startBuffer()
/**
* @param $name
*/
public function __construct($name)
{
ob_start();
return $this;
}*/
$this->name = $name;
}

protected function load($file, $data = array())
/**
* @param $file
* @param array $data
* @return $this
*/
public function buffer($file, $data = array())
{
ob_start();

Expand All @@ -31,12 +39,43 @@ protected function load($file, $data = array())
return $this;
}

protected function endBuffer()
/**
* @param $name
* @param $value
*/
public function setOutput($name, $value)
{
$this->output[$name] = $value;
}

/**
* @param $name
* @return null
*/
public function getOutput($name)
{
return isset($this->output[$name]) ? $this->output[$name] : null;
}

/**
* @return null
*/
public function __toString()
{
return $this->getOutput($this->name);
}

/**
* @return $this
*/
public function clean()
{
$output = ob_get_contents();
ob_get_clean();
ob_end_flush();
//ob_end_flush();

return $output;
$this->setOutput($this->name, $output);

return $this;
}
}
204 changes: 141 additions & 63 deletions src/Cygnite/Mvc/View/Widget.php
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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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]);
}
}

0 comments on commit 51fc671

Please sign in to comment.