From 1bcac8cf6e3a675cf0a2980fc0f2b76b9ea5accc Mon Sep 17 00:00:00 2001 From: Yami Odymel Date: Sat, 18 Jun 2016 19:45:52 +0000 Subject: [PATCH] Say hello to PJAX --- dd.html | 1 + src/autoload.php | 1 + src/getallheaders.php | 22 ++ src/initialize.php | 8 - src/main.php | 200 +++++++++++++++++- test/cache/views/default/tpls/test.phtml | 1 - .../workspace/test/default/tpls/test.phtml | 2 +- test/default/tpls/test.jade | 26 +-- test/index.php | 4 +- 9 files changed, 235 insertions(+), 30 deletions(-) create mode 100644 dd.html create mode 100644 src/getallheaders.php delete mode 100644 src/initialize.php delete mode 100644 test/cache/views/default/tpls/test.phtml diff --git a/dd.html b/dd.html new file mode 100644 index 0000000..00c18f1 --- /dev/null +++ b/dd.html @@ -0,0 +1 @@ +sdfsdfsdfsdfsdfff \ No newline at end of file diff --git a/src/autoload.php b/src/autoload.php index 6a2c473..005c0ef 100644 --- a/src/autoload.php +++ b/src/autoload.php @@ -1,5 +1,6 @@ $value) + if(substr($name, 0, 5) == 'HTTP_') + $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; + + return $headers; + } +} +?> \ No newline at end of file diff --git a/src/initialize.php b/src/initialize.php deleted file mode 100644 index b39472f..0000000 --- a/src/initialize.php +++ /dev/null @@ -1,8 +0,0 @@ - \ No newline at end of file diff --git a/src/main.php b/src/main.php index 596e2fb..11c2255 100644 --- a/src/main.php +++ b/src/main.php @@ -3,8 +3,11 @@ class Main { + private $outputBuffer = []; + function __construct($path) { + $this->startTime = microtime(true); $this->templateEngine = new \Tale\Jade\Renderer(); $path = rtrim($path, '/') . '/'; @@ -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; } @@ -27,7 +36,7 @@ function __construct($path) * @return Main */ - function setSetting($name, $value) + public function setSetting($name, $value) { switch($name) { @@ -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; @@ -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/') @@ -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)); @@ -105,6 +118,12 @@ function initialize() + //*********************************************** + //*********************************************** + //*************** C O M P I L E R *************** + //*********************************************** + //*********************************************** + /** * Compile the coffees. * @@ -128,7 +147,6 @@ public function compileCoffee() $this->scriptPath, $this->coffeeExtension, $this->compiledPath); - return $this; } @@ -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. * @@ -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. diff --git a/test/cache/views/default/tpls/test.phtml b/test/cache/views/default/tpls/test.phtml deleted file mode 100644 index 0a2d951..0000000 --- a/test/cache/views/default/tpls/test.phtml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/test/cache/views/home/ubuntu/workspace/test/default/tpls/test.phtml b/test/cache/views/home/ubuntu/workspace/test/default/tpls/test.phtml index 2ba6cf7..1fa74bc 100644 --- a/test/cache/views/home/ubuntu/workspace/test/default/tpls/test.phtml +++ b/test/cache/views/home/ubuntu/workspace/test/default/tpls/test.phtml @@ -1 +1 @@ -
ddddddddddd
\ No newline at end of file + null, 'icon' => null, 'label' => null];extract(array_replace($__scope, array_replace($__defaults, $__arguments)));?>>> '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);?> '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);?> '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);?> '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);?> '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);?> '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);?> \ No newline at end of file diff --git a/test/default/tpls/test.jade b/test/default/tpls/test.jade index 3ab89ba..b48821d 100644 --- a/test/default/tpls/test.jade +++ b/test/default/tpls/test.jade @@ -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} \ No newline at end of file +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') \ No newline at end of file diff --git a/test/index.php b/test/index.php index f12de46..d380982 100644 --- a/test/index.php +++ b/test/index.php @@ -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'); ?> \ No newline at end of file