Jump to content

framework advise


Destramic

Recommended Posts

<?php

class Action
{
	protected $_request;
	protected $_response;
	protected $_model;
	protected $_view;
		
	public function __construct(Request $request, Response $response)
	{
		$this->set_request($request)->set_response($response)->set_view($request, $response)->set_model();
		
		// if request requires partial then do this
		$partial = Partial::singleton();
		$view    = $this->get_view();
		
		$view->set_partial($partial);
		$partial->set_view(clone $view);
	}

	protected function set_request(Request $request)
	{
		$this->_request = $request;
		return $this;
	}
	
	protected function set_response(Response $response)
	{
		$this->_response = $response;
		return $this;
	}
	
	protected function set_view(Request $request, Response $response)
	{
		$this->_view = new View($request, $response);
		return $this;
	}

	public function get_request()
	{
		return $this->_request;
	}
	
	public function get_response()
	{
		return $this->_response;
	}
	
	public function get_view()
	{
		return $this->_view;
	}
	
	public function dispatch($method, $parameters)
	{
		if ($this->get_request()->is_dispatched())
		{
			if(method_exists($this, $method))
			{
				call_user_func_array(array($this, $method), $parameters);
			}
			else
			{
				echo "unable to load method";
			}
		}
	}
	
	public function __get($property)
	{
		$property = '_' . $property;

		if (property_exists($this, $property) &&
		    $property !== '$this->_request' &&
		    $property !== '$this->_responce')
		{	
			return 	$this->$property;
		}
		
		return false;
	}
	
	public function set_model()
	{		
		$class_name       = get_class($this);
		$model_name       = trim($class_name, "_Controller");
		$model_name       = strtolower($model_name);
		$property_name    = '_' . $model_name;
		$model_name       = Inflection::singularize($model_name);
		$model_name       = ucfirst($model_name);
		$model_class      = $model_name . '_Model';
		
		$this->{$property_name} = new $model_class();
	}
}

<?php

class View
{
	protected $_variables = array();
	protected $_helpers   = array();
	
	public function __construct(Request $request, Response $response)
	{
	}
		
	public function __set($name, $value)
	{
		$this->_variables[$name] = $value;
	}
	
	public function __get($name)
	{
		return $this->_variables[$name];
	}
	
	public function __call($name, $parameters)
	{	
		$helper = $this->get_helper($name);
		
		if (!$helper)
		{
			$helper = call_user_func_array(array(new $name(), '__construct'), $parameters); 
			$this->set_helper($name, $helper);
		}
	
		return $helper;
	}

	protected function set_helper($helper, $instance)
	{
		$this->_helpers[$helper] = $instance;
	}
	
	protected function get_helper($helper)
	{
		if (isset($this->_helpers[$helper]))
		{
			return $this->_helpers[$helper];
		}
		
		return false;
	}
		
	public function set_partial(Partial $partial)
	{
		if ($partial instanceof Partial)
		{
			$this->set_helper('partial', $partial);
		}
	}
	
	public function render($file)
	{
		if (preg_match("/\.html$/i", $file))
		{			
			require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file;
		}
	}
}

<?php

class Partial
{
	protected $_view;
	protected $_templates;

	protected static $_instance = null;
	
	public static function singleton()
	{
		if (self::$_instance == null)
		{
			self::$_instance = new self();
		}
	
		return self::$_instance;
	}
	
	public function set_view(View $view)
	{
		$this->_view = $view;
	}
	
	public function __set($name, $options = array())
	{
		$this->_templates[$name]; 
		$this->_templates[$name]['path']      = $options[0];
		$this->_templates[$name]['variables'] = $options[1];
	}
	
	public function __get($name)
	{
		// when $this->paertial()->header; is called in view template 
		// it extracts variables and includes template view render() method
		
		$file      = $this->_templates[$name]['path'];
		$variables = $this->_templates[$name]['variables'];
	}
}

Hey guys im trying to make alternations to my personal framework by adding a Partial class as a helper to the View class so that I'm able to add partial templates in my bootstrap and load accordingly in my view template like a header and footer.

 

what im after is a bit of advise on the functioning of my Action/View and Partial please (not sure if what im doing is really right although it does work)

 

Basically my Controller will extend my action where a model and view is made for my controller to use.  Then my partial class is sent as a helper to my view and a clone of view sent to my partial so that im able to use the view class in my partial to render my header and variables. (is this the correct way? or a good method?)

 

here's my code...like I said any advise would be greatly appreciated... thank you

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.