Skip to content

Commit

Permalink
Say hello to PJAX
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Jun 18, 2016
1 parent 567fa87 commit 1bcac8c
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 30 deletions.
1 change: 1 addition & 0 deletions dd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sdfsdfsdfsdfsdfff
1 change: 1 addition & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
require 'vendor/autoload.php';
require 'getallheaders.php';
require 'main.php';
require 'compiler/coffee.php';
require 'compiler/sass.php';
Expand Down
22 changes: 22 additions & 0 deletions src/getallheaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* getallheaders
*
* http://stackoverflow.com/questions/13224615/get-the-http-headers-from-current-request-in-php
*/

if(!function_exists('getallheaders'))
{
function getallheaders()
{
$headers = '';

foreach ($_SERVER as $name => $value)
if(substr($name, 0, 5) == 'HTTP_')
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;

return $headers;
}
}
?>
8 changes: 0 additions & 8 deletions src/initialize.php

This file was deleted.

200 changes: 196 additions & 4 deletions src/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

class Main
{
private $outputBuffer = [];

function __construct($path)
{
$this->startTime = microtime(true);
$this->templateEngine = new \Tale\Jade\Renderer();

$path = rtrim($path, '/') . '/';
Expand All @@ -13,6 +16,12 @@ function __construct($path)
$this->initialize()
->compileSass()
->compileCoffee();

if(is_array(getallheaders()))
$this->isPJAX = array_key_exists(strtolower($this->pjaxHeader), getallheaders()) ? getallheaders()[strtolower($this->pjaxHeader)]
: false;
else
$this->isPJAX = false;
}


Expand All @@ -27,7 +36,7 @@ function __construct($path)
* @return Main
*/

function setSetting($name, $value)
public function setSetting($name, $value)
{
switch($name)
{
Expand All @@ -45,6 +54,8 @@ function setSetting($name, $value)
case 'sasscPath' : $this->sasscPath = $value; break;
case 'coffeeExtension': $this->coffeeExtension = $value; break;
case 'sassExtension' : $this->sassExtension = $value; break;
case 'pjaxHeader' : $this->pjaxHeader = $value; break;
case 'titleVariable' : $this->titleVariable = $value; break;
}

return $this;
Expand All @@ -59,7 +70,7 @@ function setSetting($name, $value)
* @return Main
*/

function initialize()
private function initialize()
{
$this->setSetting('compiled' , $this->mainPath . 'compiled/')
->setSetting('script' , $this->mainPath . 'scripts/')
Expand All @@ -70,7 +81,9 @@ function initialize()
->setSetting('config' , $this->mainPath . 'config.yml')
->setSetting('coffeeExtension', '.coffee')
->setSetting('sassExtension' , '.sass')
->setSetting('extension' , '.jade');
->setSetting('extension' , '.jade')
->setSetting('titleVariable' , 'title')
->setSetting('pjaxHeader' , 'HTTP_X_PJAX');

/** Load the configures and store to the variable */
$this->config = yaml_parse(file_get_contents($this->configPath));
Expand Down Expand Up @@ -105,6 +118,12 @@ function initialize()



//***********************************************
//***********************************************
//*************** C O M P I L E R ***************
//***********************************************
//***********************************************

/**
* Compile the coffees.
*
Expand All @@ -128,7 +147,6 @@ public function compileCoffee()
$this->scriptPath,
$this->coffeeExtension,
$this->compiledPath);

return $this;
}

Expand Down Expand Up @@ -164,13 +182,181 @@ public function compileSass()
$this->sassExtension,
$this->sasscPath,
$this->compiledPath);
return $this;
}




//***********************************************
//***********************************************
//**************** C A P T U R E ****************
//***********************************************
//***********************************************

/**
* Capture the rendered content after this function.
*
* @param bool $force Force capture.
*
* @return Main
*/

public function capture($force = false)
{
if(!$this->isPJAX && !$force)
return $this;

ob_start();

return $this;
}




/**
* Stop capturing, and store the captured content to the specified array.
*
* @param string $position The place to store the captured content.
*
* @return Main
*/

public function endCapture($position = null)
{
if(!$this->isPJAX && $position)
return $this;

switch($position)
{
case 'header':
$this->outputBuffer['header'] = ob_get_clean();
break;
case 'content':
$this->outputBuffer['content'] = ob_get_clean();
break;
case 'footer':
$this->outputBuffer['footer'] = ob_get_clean();
break;
case null:
return ob_get_clean();
break;
}

return $this;
}




//***********************************************
//***********************************************
//****************** P J A X ********************
//***********************************************
//***********************************************

/**
*
*/

public function header($templateFile, $variables = null)
{
/** Set the json header if it's a PJAX request */
if($this->isPJAX)
header('Content-Type: application/json; charset=utf-8');

/** Set the title */
$this->title = isset($variables[$this->titleVariable]) ? $variables[$this->titleVariable]
: null;

/** Capture the rendered content from now on */
$this->capture()
/** Load the header and require it */
->render($templateFile, $variables)
/** Stop capturing, and store the captured content to the output buffer array */
->endCapture('header')
/** Start another capture action for the content part */
->capture();

return $this;
}

/**
*
*
*/

public function footer($templateFile, $variables = null)
{
/** Stop capturing, what we got here is a content-only part, store it either */
$this->endCapture('content')
/** Now capture the footer part */
->capture()
/** Require the footer template */
->render($templateFile, $variables)
/** And stop capturing, you know what's next right? */
->endCapture('footer');

$this->endTime = microtime(true);
$this->totalTime = $this->endTime - $this->startTime;

/** Return the rendered content if it's a PJAX request */
if($this->isPJAX)
echo json_encode($this->returnPJAX());

return $this;
}




/**
* Combine the different informations based on the PJAX header content.
*
* @return array The datas.
*/

function returnPJAX()
{
if(!$this->isPJAX)
return false;

$types = explode(', ', $this->isPJAX);
$data = [];

if(strpos($this->isPJAX, 'title') !== false)
$data['title'] = $this->title;

if(strpos($this->isPJAX, 'html') !== false)
$data['html'] = $this->outputBuffer['header'] .
$this->outputBuffer['content'] .
$this->outputBuffer['footer'];

if(strpos($this->isPJAX, 'header') !== false)
$data['header'] = $this->outputBuffer['header'];

if(strpos($this->isPJAX, 'content') !== false)
$data['content'] = $this->outputBuffer['content'];

if(strpos($this->isPJAX, 'footer') !== false)
$data['footer'] = $this->outputBuffer['footer'];

if(strpos($this->isPJAX, 'wasted') !== false)
$data['wasted'] = $this->totalTime;

return $data;
}




//***********************************************
//***********************************************
//****************** B A S I C ******************
//***********************************************
//***********************************************

/**
* Render the template files and output.
*
Expand Down Expand Up @@ -207,6 +393,12 @@ public function fetch($templateFile, $variables = null)



//***********************************************
//***********************************************
//**************** H E L P E R S ****************
//***********************************************
//***********************************************

/**
* Get the path of the shortname,
* returns the original path if there's no shortname for the path.
Expand Down
1 change: 0 additions & 1 deletion test/cache/views/default/tpls/test.phtml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
<html><body><div class="ts test">ddddddddddd</div><?php /* ddddd */ ?><select class="ts medium dropdown" data-setting-name="language"><?php $__iterator0 = isset($items) ? $items : [];?><?php foreach ($__iterator0 as $item) {?><option><?=htmlentities(isset($item) ? $item : '', \ENT_QUOTES, 'UTF-8')?></option><?php }?><?php unset($__iterator0);?></select><?php /* ddddd */ ?><select class="ts medium dropdown" data-setting-name="language"><?php $__iterator1 = isset($items) ? $items : [];?><?php foreach ($__iterator1 as $item) {?><option><?=htmlentities(isset($item) ? $item : '', \ENT_QUOTES, 'UTF-8')?></option><?php }?><?php unset($__iterator1);?></select></body></html>
<?php $__mixins = [];?><?php $__mixins['item'] = function(array $__arguments, array $__scope) {$__defaults = ['link' => null, 'icon' => null, 'label' => null];extract(array_replace($__scope, array_replace($__defaults, $__arguments)));?><a class="item"<?php $__value = isset($link) ? $link : false; if (!\Tale\Jade\Compiler\is_null_or_false($__value)) echo ' href='.\Tale\Jade\Compiler\build_value($__value, '"', true); unset($__value);?>><span<?php $__values = ["icon", isset($icon) ? $icon : false]; if (!\Tale\Jade\Compiler\is_array_null_or_false($__values)) echo ' class='.\Tale\Jade\Compiler\build_class_value($__values, '"', false); unset($__values);?>></span><?=htmlentities($label, \ENT_QUOTES, 'UTF-8')?></a><?php };?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore); $__mixinCallArgs = ['link' => 'photo', 'icon' => 'icon--picture', 'label' => 'A'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore); $__mixinCallArgs = ['link' => 'photo', 'icon' => 'icon--circles', 'label' => 'B'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore); $__mixinCallArgs = ['link' => 'photo', 'icon' => 'icon--music', 'label' => null, 0 => 'C'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore); $__mixinCallArgs = ['link' => 'photo', 'icon' => 'icon--draw', 'label' => null, 0 => 'D'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore); $__mixinCallArgs = ['link' => 'photo', 'icon' => 'icon--dna', 'label' => null, 0 => 'E'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?><?php $__ignore = array_flip([0 => 'GLOBALS', 1 => '_SERVER', 2 => '_GET', 3 => '_POST', 4 => '_FILES', 5 => '_REQUEST', 6 => '_SESSION', 7 => '_ENV', 8 => '_COOKIE', 9 => 'php_errormsg', 10 => 'HTTP_RAW_POST_DATA', 11 => 'http_response_header', 12 => 'argc', 13 => 'argv', 14 => '__scope', 15 => '__arguments', 16 => '__ignore', 17 => '__block']);$__scope = array_diff_key(array_replace(get_defined_vars(), $__ignore), $__ignore); $__mixinCallArgs = ['link' => 'photo', 'icon' => 'icon--gamepad', 'label' => 'F'];call_user_func($__mixins['item'], $__mixinCallArgs, $__scope);unset($__ignore);unset($__scope);unset($__mixinCallArgs);?>
26 changes: 11 additions & 15 deletions test/default/tpls/test.jade
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
html
body
.ts.test
| ddddd
| dddddd

//- ddddd
select.ts.medium.dropdown(data-setting-name='language')
each $item in $items
option #{$item}

//- ddddd
select.ts.medium.dropdown(data-setting-name='language')
each $item in $items
option #{$item}
mixin item(link, icon, label)
a.item(href=$link)
span.icon(class=$icon)
$label

+item('photo', 'icon--picture', 'A')
+item('photo', 'icon--circles', 'B')
+item('photo', 'icon--music' , 'C')
+item('photo', 'icon--draw' , 'D')
+item('photo', 'icon--dna' , 'E')
+item('photo', 'icon--gamepad', 'F')
4 changes: 3 additions & 1 deletion test/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
include '../src/autoload.php';

$Avane = new Avane\Main(__DIR__ . '/default');
$Avane->render('Ello', ['items' => [1, 2, 3, 4, 5, 6]]);
$Avane->header('Ello', ['title' => 'dsfsdfsdfsd'])
->render('Ello', ['items' => range(0, 1000)])
->footer('Ello');
?>

0 comments on commit 1bcac8c

Please sign in to comment.