Maslosoft Addendum API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
<?php
/**
* This software package is licensed under AGPL, Commercial license.
*
* @package maslosoft/addendum
* @licence AGPL, Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com> (Meta container, further improvements, bugfixes)
* @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
* @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
* @link https://maslosoft.com/addendum/ - maslosoft addendum
* @link https://code.google.com/p/addendum/ - original addendum project
*/
namespace Maslosoft\Addendum\Cache;
use Maslosoft\Addendum\Addendum;
use Maslosoft\Addendum\Helpers\SoftIncluder;
use Maslosoft\Addendum\Options\MetaOptions;
use Maslosoft\Cli\Shared\Helpers\PhpExporter;
/**
* Meta Namespaces cache
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class NsCache
{
const FileName = '_ns.php';
private $file = '';
/**
* Addendum instance
* @var Addendum
*/
private $ad = null;
/**
*
* @var
*/
private static $nsCache = [];
/**
* Hash map of namespaces.
* Namespaces is key, while value is boolean and is not really relevant
* @var array
*/
private $namespaces;
/**
* Option holder
* @var MetaOptions
*/
private $options = null;
/**
* @internal Flag that will trigger cache validity check for namespaces cache
* But only if no options provided
* @var bool
*/
public static $addeNs = true;
public function __construct($path, Addendum $addendum, MetaOptions $options = null)
{
$this->file = sprintf('%s/%s', $path, self::FileName);
$this->namespaces = $addendum->nameKeys;
$this->ad = $addendum;
$this->setOptions($options);
}
public function setOptions(MetaOptions $options = null)
{
if (!empty($options) && !empty($options->namespaces))
{
foreach ($options->namespaces as $ns)
{
if (empty($this->namespaces[$ns]))
{
self::$addeNs = true;
$this->namespaces[$ns] = true;
}
}
}
}
public function valid()
{
$ns = $this->get();
$valid = $this->isValid($ns);
// Ovverride existing cache if not valid
if (!$valid)
{
$this->set();
}
return $valid;
}
public function get()
{
if (!empty(self::$nsCache[$this->file]))
{
return self::$nsCache[$this->file];
}
self::$nsCache[$this->file] = SoftIncluder::includeFile($this->file);
return self::$nsCache[$this->file];
}
public function set()
{
$ns = [];
foreach (array_keys($this->namespaces) as $name)
{
$ns[$name] = true;
}
$data = PhpExporter::export($ns);
$mask = umask(0);
file_put_contents($this->file, $data);
umask($mask);
self::$nsCache[$this->file] = $ns;
}
public function remove()
{
unset(self::$nsCache[$this->file]);
if (file_exists($this->file))
{
unlink($this->file);
}
}
private function isValid($ns)
{
// Fresh data
if (empty($ns))
{
return true;
}
// Additional check if added namespace manually
if (empty($this->options) && self::$addeNs)
{
$addendumDiff = array_diff_key($this->ad->nameKeys, $this->namespaces);
if (!empty($addendumDiff))
{
// Add missing namespaces to cache them
foreach (array_keys($addendumDiff) as $ns)
{
$this->namespaces[$ns] = true;
}
self::$addeNs = false;
return false;
}
}
// Check if has all namespaces
$cachedDiff = array_diff_key($this->namespaces, $ns);
if (empty($cachedDiff))
{
self::$addeNs = false;
return true;
}
return false;
}
}
API documentation generated by ApiGen