Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkiernan120 committed Oct 9, 2018
1 parent 3d999ca commit ff8e911
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 93 deletions.
23 changes: 12 additions & 11 deletions src/Guacamole.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Guacamole
public $template;

/**
* Guacamole Cons
* [__construct description]
* @param array $config config settings for Guacamole;
*/
public function __construct( array $config = array() )
{
Expand All @@ -32,18 +33,18 @@ public function __construct( array $config = array() )
}

/**
* [setConfig description]
* @param array $config [description]
* config setter
* @param array $config array of config options
*/
public function setConfig( array $config ) :void
{
$this->config = $config;
}

/**
* [getConfig description]
* @param [type] $name [description]
* @return [type] [description]
* config getter
* @param mixed $name Optional name parameter can be used to return one config setting
* @return mixed returns whole config array or singular config option
*/
public function getConfig( $name = null )
{
Expand All @@ -56,12 +57,12 @@ public function getConfig( $name = null )
}

/**
* [render description]
* @param string $templateString [description]
* @param [type] $params [description]
* @return [type] [description]
* Render a template
* @param string $templateString A template string to render
* @param array $params optional array of parameters to pass through with the template
* @return string return the template string after render and processs
*/
public function render( string $templateString, $params = null )
public function render( string $templateString, $params = null ) :string
{
if( is_array( $params ) && !empty( $params ) ){
if( isset( $params["tags"] ) ){
Expand Down
81 changes: 45 additions & 36 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<?php
/**
*
Expand All @@ -14,7 +13,8 @@
*/
class Tag
{



public $tags = array();
public $guacamole;

Expand Down Expand Up @@ -104,54 +104,28 @@ public function tagExists( string $tag, $template = null ) :bool
*/
public function proccessTag( $tag, $params = null ) // TODO: move params check to be a bit more intuitive
{

$params = Clean::clean( $params );

$data = array();

if( $this->tagExists( $tag ) ){

if( Util::isString( $params ) ){
$this->guacamole->template->setTemplate( str_ireplace( "<{$tag}>", trim( $params ), $this->guacamole->template->getTemplate() ) );
return;
$data = $this->processString( $params );
}
else if( is_array( $params ) && !empty( $params ) ){

if( isset( $params["function"] ) && is_callable( $params["function"] ) ){
$this->setTemplate( str_ireplace( "<{$tag}>", call_user_func_array( $params["function"], @$params["params"] ), $this->guacamole->template->getTemplate() ) );
return;
}
if( is_array( $params ) && !empty( $params ) ){
$data = $this->processArray( $params );
}

if( isset( $params["controller"] ) && isset( $params["method"] ) ){


if( class_exists( $params["controller"] ) ){
$controller = new $params["controller"]( @$params["controllerPassin"] );

if( method_exists( $controller, $params["method"] ) ){
$data = $controller->{$params["method"]}( @$params["passin"] );
}
}
}


if( isset( $params["path"] ) && Util::fileExists( $params["path"].$tag ) ){

ob_start();
require "{$params["path"]}{$tag}.php";
$string = ob_get_clean();
$this->guacamole->template->setTemplate( str_ireplace( "<{$tag}>", trim( $string ), $this->guacamole->template->getTemplate() ) );
return;
}

}

if( is_callable( $params ) ){
$this->guacamole->template->setTemplate( str_ireplace( "<{$tag}>", call_user_func_array( $params , $params_arr = array() ), $this->guacamole->template->getTemplate() ) );
return;
}
else {
$this->guacamole->template->setTemplate( str_ireplace( "<{$tag}>", "", $this->getTemplate() ) );
return;
}
}
}

Expand All @@ -171,4 +145,39 @@ public function process() :void
}
}

}
public function processString( string $string )
{
if( Util::isString( $string ) ){
$this->guacamole->template->setTemplate( str_ireplace( "<{$tag}>", trim( $params ), $this->guacamole->template->getTemplate() ) );
}
}

public function processArray( array $array )
{
$data = array();
$returnData = array();

if( is_array( $array ) && !empty( $array ) ){

if( isset( $array["function"] ) && is_callable( $array["function"] ) ){
$parameters = ( ( isset( $array["parameters"] ) && is_array( $array["parameters"] ) ) ? $array["parameters"] : array());
$data = call_user_func_array( $array["function"], $parameters );
$returnData = array_merge( $returnData, is_array( $data ) ? $data : array( $data ) );
}

if( isset( $array["controller"] ) && isset( $array["method"] ) ){
if( class_exists( $array["controller"] ) ) {
$controllerText = $array["controller"];
$controller = new $controllerText( @$array["controllerPassin"] );

if( method_exists( $controller, $array["method"] ) ){
$data = $controller->{$array["method"]}( @$array["passin"] );
$returnData = array_merge( $returnData, is_array( $data ) ? $data : array( $data ) );
}
}
}
}
return $returnData;
}

}
56 changes: 26 additions & 30 deletions src/Template/Template.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
<?php

namespace Guacamole;
namespace Guacamole\Template;


/**
* summary
*/
class Clean
class Template
{
public $template;
public $guacamole;

/**
* [clean description]
* @param [type] $var [description]
* @return [type] [description]
* summary
*/
public static function clean( $var )
{

if( is_array( $var ) && !empty( $var ) ){
foreach( $var as $key => $value ){
$var[$key] = self::clean( $value );
}
}
public function __construct( \Guacamole\Guacamole $guacamole )
{
$this->guacamole = $guacamole;
}

$type = gettype( $var );
if( method_exists( __CLASS__, $type ) ){
return self::{$type}( $var );
}
else {
return $var;
}
return self::{$type}( $var );
}
/**
* [getTemplate description]
* @return [type] [description]
*/
public function getTemplate() :string
{
return (string)$this->template;
}

/**
* [text description]
* @param [type] $variable [description]
* @return [type] [description]
*/
public static function text( $variable ){
return trim( str_replace( "\n", "", htmlspecialchars( strip_tags( trim( $variable ) ), ENT_QUOTES, "UTF-8" ) ) );
/**
* [setTemplate description]
* @param string $template [description]
*/
public function setTemplate( string $template ) :string
{
return $this->template = $template;
}

}
}
43 changes: 27 additions & 16 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,57 @@ public function __construct()
}

/**
* [fileExists description]
* @param string $path [description]
* @param string $ext [description]
* @return [type] [description]
* Check if file exists
* @param string $path path to file will accept relative paths
* @param string $ext optional extension does assume file is php
* @return bool [description]
*/
public static function fileExists( string $path, string $ext = "php" )
public static function fileExists( string $path, string $ext = "php" ) : bool
{
return (bool)file_exists( $path . "." . $ext );
}

/**
* [getFileContents description]
* @param string $path [description]
* @param string $ext [description]
* @return [type] [description]
* Get the contents of a file
* @param string $path path to file will accept relative paths
* @param string $ext optional extension does assume file is php
* @return mixed returns files contents or false if file not found;
*/
public static function getFileContents( string $path, string $ext = "php" )
{
return file_get_contents( "{$path}.{$ext}" );
if( self::fileExists( $path, $ext ) ){
return file_get_contents( "{$path}.{$ext}" );
}
return false;
}

/**
* [isString description]
* @param [type] $variable [description]
* @return boolean [description]
* check if variable is a string
* @param mixed $variable mixed variable to check
* @return boolean returns boolean
*/
public static function isString( $variable ) :bool
{
return (bool)is_string( $variable );
}

/**
* [isArray description]
* @param [type] $variable [description]
* @return boolean [description]
* check if variable is array
* @param miaxed $variable mixed variable to check
* @return boolean returns boolean
*/
public static function isArray( $variable ) :bool
{
return (bool)is_array( $variable );
}

/**
* find a file in directorty
* @param string $file file name
* @param string $directory directory path defaults to current working directory
* @return string if fount returns string path to file else return false
* @example Util::findFile( 'Tag.php' ); //output './Tag/Tag.php'
*/
public static function findFile( string $file, $directory = "." )
{
$files = scandir( $directory );
Expand All @@ -73,6 +83,7 @@ public static function findFile( string $file, $directory = "." )
self::findFile( $file, $path );
}
}
return false;
}

}

0 comments on commit ff8e911

Please sign in to comment.