A super easy PHP Framework for web development! https://github.com/exacti/phacil-framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
5.1 KiB

6 years ago
<?php
/*
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
* GPLv3 General License.
* https://exacti.com.br
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
*/
namespace Phacil\Framework;
use Phacil\Framework\Config;
/** @package Phacil\Framework */
6 years ago
class Document {
private $title;
private $description;
private $keywords;
private $links = array();
private $styles = array();
private $scripts = array();
private $fbmetas = array();
/**
* @param string $title
* @return void
*/
6 years ago
public function setTitle($title) {
if(Config::PATTERSITETITLE()) {
$this->title = sprintf($title, Config::PATTERSITETITLE());
6 years ago
} else {
$this->title = $title;
}
}
/** @return string */
6 years ago
public function getTitle() {
return $this->title;
}
/**
* @param string $description
* @return void
*/
6 years ago
public function setDescription($description) {
$this->description = $description;
}
/** @return string */
6 years ago
public function getDescription() {
return $this->description;
}
/**
* @param string $keywords
* @return void
*/
6 years ago
public function setKeywords($keywords) {
$this->keywords = $keywords;
}
/** @return string */
6 years ago
public function getKeywords() {
return $this->keywords;
}
/**
* @param string $href
* @param string $rel
* @return void
*/
6 years ago
public function addLink($href, $rel) {
$this->links[md5($href)] = array(
'href' => $href,
'rel' => $rel
);
}
/** @return array */
6 years ago
public function getLinks() {
return $this->links;
}
/**
* @param string $var
* @return string
*/
private function checkCDN( $var) {
6 years ago
if(Config::CDN()) {
6 years ago
if($this->checkLocal($var)){
$var = Config::CDN().$var;
6 years ago
}
}
return $var;
}
/**
* @param string $href
* @param string $rel
* @param string $media
* @param bool $minify
* @return void
*/
6 years ago
public function addStyle($href, $rel = 'stylesheet', $media = 'screen', $minify = true) {
if ($minify) $href = $this->cacheMinify($href, 'css');
$href = $this->checkCDN($href);
$this->styles[md5($href)] = array(
'href' => $href,
'rel' => $rel,
'media' => $media
);
}
/** @return array */
6 years ago
public function getStyles() {
return $this->styles;
}
/**
* @param string $script
* @param int|string $sort
* @param bool $minify
* @return void
*/
public function addScript($script, $sort = 0, $minify = true) {
6 years ago
if($minify) $script = $this->cacheMinify($script, 'js');
$script = $this->checkCDN($script);
$this->scripts[($sort)][md5($script)] = $script;
}
/** @return array */
6 years ago
public function getScripts() {
$a = $this->scripts;
ksort($a);
foreach($a as $value){
foreach($value as $key => $value){
$b[$key] = $value;
}
}
return (isset($b)) ? $b : [];
6 years ago
}
/**
* @param string $property
* @param string $content
* @return void
*/
6 years ago
public function addFBMeta($property, $content = ''){
$this->fbmetas[md5($property)] = array(
'property' => $property,
'content' => $content
);
}
/** @return array */
6 years ago
public function getFBMetas(){
return $this->fbmetas;
}
/**
* @param string $val
* @return bool
*/
6 years ago
private function checkLocal ($val) {
$testaProtocolo = substr($val, 0, 7);
return ($testaProtocolo != "http://" && $testaProtocolo != "https:/");
6 years ago
}
/**
* @param string $ref
* @param string $type
* @return string
*/
6 years ago
private function cacheMinify($ref, $type) {
$dir = "css-js-cache/";
$dirCache = Config::DIR_PUBLIC(). $dir;
6 years ago
$newName = str_replace("/", "_", $ref);
$file = Config::DIR_PUBLIC().$ref;
6 years ago
$cachedFile = $dirCache.$newName;
$cacheFile = $dir.$newName;
if(!$this->checkLocal($ref)) {
return $ref;
}
if (!file_exists($dirCache)) {
mkdir($dirCache, 0755, true);
}
if (file_exists($file) and Config::CACHE_MINIFY()) {
6 years ago
if($type == "js") {
if(file_exists($cachedFile) and Config::CACHE_JS_CSS()) {
6 years ago
return $cacheFile;
} else {
include_once Config::DIR_SYSTEM()."ecompress/JSMin.php";
6 years ago
$buffer = file_get_contents($file);
$buffer = preg_replace('/<!--(.*)-->/Uis', '', $buffer);
$buffer = \JSMin::minify($buffer);
6 years ago
file_put_contents($cachedFile, $buffer);
return $cacheFile;
}
}elseif($type == "css") {
if(file_exists($cachedFile) && Config::CACHE_JS_CSS()) {
6 years ago
return $cacheFile;
} else {
include_once Config::DIR_SYSTEM()."ecompress/cssMin.php";
6 years ago
$buffer = file_get_contents($file);
$buffer = minimizeCSS($buffer);
file_put_contents($cachedFile, $buffer);
return $cacheFile;
}
} else {
return $ref;
}
} else {
return $ref;
}
}
}