Maslosoft Mangan 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 170
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Helpers;
use Maslosoft\Addendum\Addendum;
use Maslosoft\Addendum\Helpers\SoftIncluder;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Cli\Shared\Helpers\PhpExporter;
use Maslosoft\Mangan\Helpers\Index\IndexModel;
use Maslosoft\Mangan\Mangan;
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\ManganTest\Extensions\IndexMetaCleaner;
class IndexManager
{
const IndexTypeHashed = 'hashed';
const IndexType2dSphere = '2dsphere';
const DefaultInstanceId = 'indexManager';
private static $instances = [];
private static $paths = [];
/**
* NOTE: This is public because of IndexMetaCleaner testing extension
*
* DO NOT TOUCH!
*
* @see IndexMetaCleaner
* @internal
* @var array
*/
public static $haveIndex = [];
/**
* NOTE: This is public because of IndexMetaCleaner testing extension
*
* DO NOT TOUCH!
*
* @see IndexMetaCleaner
* @internal
* @var bool
*/
public static $haveDir = false;
/**
* Create flyweight instance of index manager
* @param string $instanceId
* @return static
*/
public static function fly($instanceId = self::DefaultInstanceId)
{
if (empty(self::$instances[$instanceId]))
{
self::$instances[$instanceId] = new static();
}
return self::$instances[$instanceId];
}
public function create(AnnotatedInterface $model)
{
$className = get_class($model);
// If have or don't have indexes skip further checks
if(array_key_exists($className, self::$haveIndex))
{
return self::$haveIndex[$className];
}
$fieldMetas = ManganMeta::create($model)->fields();
// Filter out fields without index
foreach($fieldMetas as $key => $metaProperty)
{
if(empty($metaProperty->index))
{
unset($fieldMetas[$key]);
}
}
// Does not have indexes, mark as index-less
if(empty($fieldMetas))
{
self::$haveIndex[$className] = false;
return false;
}
$path = $this->getStoragePath($model, $className);
$data = SoftIncluder::includeFile($path);
if(!empty($data))
{
return true;
}
$results = [];
$indexes = [];
foreach($fieldMetas as $fieldMeta)
{
if(empty($fieldMeta->index))
{
continue;
}
/* @var $fieldMeta DocumentPropertyMeta */
foreach($fieldMeta->index as $indexMeta)
{
$index = new IndexModel($model, $indexMeta);
$results[] = (int)$index->apply();
$indexes[] = $index->getIndexes();
}
}
self::$haveIndex[$className] = true;
$dir = dirname($path);
if(!self::$haveDir && !file_exists($dir))
{
self::$haveDir = mkdir($dir);
}
file_put_contents($path, PhpExporter::export($indexes, 'Auto generated, do not modify'));
@chmod($path, 0666);
return array_sum($results) === count($results);
}
public function getStoragePath(AnnotatedInterface $model = null, $className = null)
{
if(empty($className))
{
$className = __CLASS__;
}
if(empty(self::$paths[$className]))
{
if(empty($model))
{
$mn = Mangan::fly();
}
else
{
$mn = Mangan::fromModel($model);
}
$params = [
Addendum::fly()->runtimePath,
str_replace('\\', '.', static::class),
$mn->connectionId,
$mn->dbName,
str_replace('\\', '.', $className),
];
self::$paths[$className] = vsprintf('%s/%s/%s.%s@%s.php', $params);
}
return self::$paths[$className];
}
}
API documentation generated by ApiGen