diff --git a/src/Cygnite/Mvc/View/Output.php b/src/Cygnite/Mvc/View/Output.php
index f6869d4..817b015 100644
--- a/src/Cygnite/Mvc/View/Output.php
+++ b/src/Cygnite/Mvc/View/Output.php
@@ -1,22 +1,30 @@
name = $name;
+ }
- protected function load($file, $data = array())
+ /**
+ * @param $file
+ * @param array $data
+ * @return $this
+ */
+ public function buffer($file, $data = array())
{
ob_start();
@@ -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;
}
}
\ No newline at end of file
diff --git a/src/Cygnite/Mvc/View/Widget.php b/src/Cygnite/Mvc/View/Widget.php
index df425e4..c388e73 100644
--- a/src/Cygnite/Mvc/View/Widget.php
+++ b/src/Cygnite/Mvc/View/Widget.php
@@ -1,112 +1,190 @@
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)
- * Whether a offset exists
- *
- * @link http://php.net/manual/en/arrayaccess.offsetexists.php
- * @param mixed $offset
- * An offset to check for. - *
- * @return boolean true on success or false on failure. - * - *
- * 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)
- * Offset to retrieve
- * @link http://php.net/manual/en/arrayaccess.offsetget.php
- * @param mixed $offset
- * The offset to retrieve. - *
- * @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)- * The offset to assign the value to. - *
- * @param mixed $value- * The value to set. - *
- * @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)- * The offset to unset. - *
- * @return void + * ArrayAccess + * @param int|string $offset */ public function offsetUnset($offset) { - // TODO: Implement offsetUnset() method. - }} \ No newline at end of file + unset($this->data[$offset]); + } +}