Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Move source loading from engine to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtree committed Feb 4, 2020
1 parent 79170ea commit 909041f
Show file tree
Hide file tree
Showing 6 changed files with 1,812 additions and 1,872 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ It instantiates DomElements as customizable backend components and orchestrates
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.2-8892BF.svg?style=flat-square)](https://php.net/)
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/hxtree)


# Usage

LHTML5
```PHP
<?php require $_SERVER['DOCUMENT_ROOT'] . '/src/Autoloader.php'; ?>
<body>
<partial name="userprofile">
<block name="userprofile">
<p><var name="username"/></p>
</partial>
</block>
</body>
```
HTML5
Expand Down
12 changes: 11 additions & 1 deletion src/Builder/DynamicPageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ class DynamicPageBuilder implements BuilderInterface
*/
public function createObject(array $parameters): ?bool
{
$this->engine = new Engine($parameters);
// determine source
if (isset($parameters['filename'])) {
$source = file_get_contents($parameters['filename']);
} elseif (isset($parameters['markup'])) {
$source = $parameters['markup'];
} else {
$source = '';
}

// create engine pass source
$this->engine = new Engine($source);

// instantiate dynamic elements
if (is_array($parameters['handlers'])) {
Expand Down
12 changes: 11 additions & 1 deletion src/Builder/StaticPageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ class StaticPageBuilder implements BuilderInterface
*/
public function createObject(array $parameters): ?bool
{
$this->engine = new Engine($parameters);
// determine source
if (isset($parameters['filename'])) {
$source = file_get_contents($parameters['filename']);
} elseif (isset($parameters['markup'])) {
$source = $parameters['markup'];
} else {
$source = '';
}

// create engine pass source
$this->engine = new Engine($source);

return true;
}
Expand Down
78 changes: 0 additions & 78 deletions src/Builder/StaticTemplateBuilder.php

This file was deleted.

76 changes: 27 additions & 49 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ class Engine
// Document Object Model (DOM)
public $dom;

// DOM doctype
public $doctype;

// DOM XPath Query object
public $xpath;

Expand All @@ -46,21 +43,34 @@ class Engine
];

// Component placeholder ID attribute
private $element_index_attribute = '_livingMarkup_ref';

// DomDocument LibXML debug
private $libxml_debug = false;
private $element_index_attribute = '_LivingMarkup_Ref';

/**
* Page constructor
*
* @param array $parameters
* @param string $source
*/
public function __construct(array $parameters)
public function __construct(string $source)
{

// suppress xml parse errors unless debugging
libxml_use_internal_errors(true);

$this->createDom($source);
}


/**
* Custom load page wrapper for server side HTML5 entity support
* (cannot use $this->dom->loadHTMLFile as it removes HTML5 entities, such as &copy;)
*
* @param string $source
*/
public function createDom(string $source): void
{

// TODO: turn into own object

// create a document object model
$this->dom = new DomDocument();

Expand All @@ -79,51 +89,19 @@ public function __construct(array $parameters)
// DomDocument encoding
$this->dom->encoding = 'UTF-8';

// set doctype string for loading DOM
$this->setDoctype();

// load DOM from filename
$this->loadDom($parameters);
}

/**
* Set an HTML5 doctype string property with HTML entities (&copy; etc.) included
*
* @return void
*/
public function setDoctype(): void
{

$entities = new Entities;
$entity = $entities->get();
$this->doctype = '<!DOCTYPE html [' . $entity . ']> ';
}

/**
* Custom load page wrapper for server side HTML5 entity support
* (cannot use $this->dom->loadHTMLFile as it removes HTML5 entities, such as &copy;)
*
* @param array $parameters
*/
public function loadDom(array $parameters): void
{
// get source file as string
if (isset($parameters['filename'])) {
$source = file_get_contents($parameters['filename']);
} elseif (isset($parameters['markup'])) {
$source = $parameters['markup'];
} else {
return;
}
// add DOCTYPE declaration
$doctype = '<!DOCTYPE html [' . Entities::HTML5 . ']>'. PHP_EOL;

// convert source file to Document Object Model for manipulation
// set doctype if $this->dom->doctype would not get set
// replace DOCTYPE if present
$count = 1;
str_ireplace('<!doctype html>', $this->doctype, $source, $count);
str_ireplace('<!doctype html>', $doctype, $source, $count);
if ($count == 0) {
$source = $this->doctype . $source;
// add doctype if not present
$source = $doctype . $source;
// load as XML
$this->dom->loadXML($source);
} else {
// load as HTML
$this->dom->loadHTML($source);
}

Expand Down

0 comments on commit 909041f

Please sign in to comment.