Initial commit

This commit is contained in:
Abel Hoogeveen 2020-11-26 22:02:36 +01:00
commit 8f3a2ba826
Signed by: abelhooge
GPG Key ID: 387E8DC1F73306FC
12 changed files with 585 additions and 0 deletions

0
.drone.yml Normal file
View File

4
.gitattributes vendored Normal file
View File

@ -0,0 +1,4 @@
.gitattributes export-ignore
.gitignore export-ignore
.drone.yml export-ignore
test/ export-ignore

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
composer.lock
composer.phar
.idea/
build/
test/temp/
vendor/

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2013-2020 i15
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

28
composer.json Normal file
View File

@ -0,0 +1,28 @@
{
"name": "fuzeworks/objectstorage",
"minimum-stability": "stable",
"license": ["MIT"],
"authors": [
{
"name": "Abel Hoogeveen",
"email": "abel@i15.nl"
}
],
"require": {
"php": ">=7.4.0",
"fuzeworks/core": "~1.2",
"psr/cache": "~1.0"
},
"suggest": {
"ext-redis": "1"
},
"require-dev": {
"phpunit/phpunit": "^9",
"fuzeworks/tracycomponent": "~1.2"
},
"autoload": {
"psr-4": {
"FuzeWorks\\ObjectStorage\\": "src/FuzeWorks/ObjectStorage/"
}
}
}

36
config.storage.php Normal file
View File

@ -0,0 +1,36 @@
<?php
/**
* FuzeWorks ObjectStorage Component.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
return [];

View File

@ -0,0 +1,40 @@
<?php
/**
* FuzeWorks ObjectStorage Component.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
namespace FuzeWorks\ObjectStorage\Exception;
class ObjectStorageException extends \FuzeWorks\Exception\CoreException
{
}

View File

@ -0,0 +1,69 @@
<?php
/**
* FuzeWorks ObjectStorage Component.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
namespace FuzeWorks\ObjectStorage;
use FuzeWorks\Configurator;
use FuzeWorks\Factory;
use FuzeWorks\iComponent;
/**
* ObjectStorageComponent Class.
*
* This class doesn't do very much, except show the FuzeWorks\Configurator that this dependency has been loaded.
*
* @author i15
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
*/
class ObjectStorageComponent implements iComponent
{
public function getName(): string
{
return 'ObjectStorageComponent';
}
public function getClasses(): array
{
return ['storage' => $this];
}
public function onAddComponent(Configurator $configurator)
{
}
public function onCreateContainer(Factory $container)
{
}
}

View File

@ -0,0 +1,268 @@
<?php
/**
* FuzeWorks ObjectStorage Component.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
namespace FuzeWorks\ObjectStorage\Provider;
use FuzeWorks\ObjectStorage\Exception\ObjectStorageException;
use FuzeWorks\ObjectStorage\iObjectStorageProvider;
use FuzeWorks\Core;
use Traversable;
/**
* FileProvider
*
* An ObjectStorage provider which saves objects in files using the serialize() and unserialize() functions.
* The easiest to use StorageProvider, though with its caveats. It's nimble and works on nearly everything, but
* not particularly fast, and has some risks when multiple processes use the data at the same time.
*
* @todo Figure out a way to prevent indexes from clashing in multiple threads
* @todo Find a better way to sanitize key names other than hashing them using crc32
* @author i15
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
*/
class FileProvider implements iObjectStorageProvider
{
/**
* The directory where all objects are saved
*
* @var string
*/
protected string $directory;
/**
* The file where the index for this provider is saved
*
* @var string
*/
protected string $indexFile;
/**
* The index of all objects stored
*
* @var array
*/
protected array $index;
/**
* @param array $providerConfig
* @return bool
* @throws ObjectStorageException
*/
public function init(array $providerConfig): bool
{
// First load the directory from the providerConfig
$directory = isset($providerConfig['storageDirectory']) ? $providerConfig['storageDirectory'] : null;
// Check if the directory exists
if (!file_exists($directory) || !is_dir($directory))
throw new ObjectStorageException("Could not load FileProvider ObjectStorage. Provided storageDirectory is not a directory.");
// Check if that directory also is writeable
if (!Core::isReallyWritable($directory))
throw new ObjectStorageException("Could not load FileProvider ObjectStorage. Provided storageDirectory is not writeable.");
// Save the directory and indexFile, and load or initialize the indexFile
$this->directory = rtrim($directory);
$this->indexFile = $this->directory . DS . 'index.fwstorage';
// If the index does not exist yet, load it
if (!file_exists($this->indexFile))
{
if (!$this->write_file($this->indexFile, serialize(['index' => []])))
throw new ObjectStorageException("Could not load FileProvider ObjectStorage. Could not write index.");
chmod($this->indexFile, 0640);
}
// And finally, load the index
$this->index = unserialize(file_get_contents($this->indexFile))['index'];
return true;
}
public function getIndex(): array
{
return $this->index;
}
public function getItem(string $key): ?array
{
// Convert the key
$file = $this->directory . DS . crc32($key) . '.fwstorage';
// If the key could not be found in the index, return null
if (!isset($this->index[$key]))
return null;
// Check if the file exists. If not, delete the indexed value
if (!file_exists($file))
{
$this->deleteItem($key);
return null;
}
// Otherwise try and load the metaData and contents
$meta = $this->index[$key]['meta'];
$data = unserialize(file_get_contents($file));
// Return the combined data
return ['meta' => $meta, 'data' => $data];
}
public function getItemMeta(string $key): ?array
{
// If the key could not be found in the index, return null
if (!isset($this->index[$key]))
return null;
// Otherwise return the meta data
return $this->index[$key]['meta'];
}
public function getItems(array $keys = []): array
{
$output = [];
foreach ($keys as $key)
$output[$key] = $this->getItem($key);
return $output;
}
public function hasItem(string $key): bool
{
return isset($this->index[$key]);
}
public function clear(): bool
{
$keys = array_keys($this->index);
return $this->deleteItems($keys);
}
public function deleteItem(string $key): bool
{
// Convert the key
$file = $this->directory . DS . crc32($key) . '.fwstorage';
// Remove the file first
if (file_exists($file))
unlink($file);
// And remove it from the index
if (isset($this->index[$key]))
unset($this->index[$key]);
// And commit the index
return $this->commitIndex();
}
public function deleteItems(array $keys): bool
{
foreach ($keys as $key)
$this->deleteItem($key);
return true;
}
public function save(string $key, $value, array $metaData = []): bool
{
// Convert the key
$file = $this->directory . DS . crc32($key) . '.fwstorage';
// Remove the file if it already exists
if (file_exists($file))
unlink($file);
// Write everything to the index
$this->index[$key] = ['meta' => $metaData];
$this->commitIndex();
// And write the contents
if ($this->write_file($file, serialize($value))) {
chmod($file, 0640);
return true;
}
return false;
}
public function commit(): bool
{
// TODO: Implement commit() method.
}
public function revert(): bool
{
// TODO: Implement revert() method.
}
private function commitIndex(): bool
{
if ($this->write_file($this->indexFile, serialize(['index' => $this->index]))) {
chmod($this->indexFile, 0640);
return true;
}
return false;
}
/**
* Write File
*
* Writes data to the file specified in the path.
* Creates a new file if non-existent.
*
* @param string $path File path
* @param string $data Data to write
* @param string $mode fopen() mode (default: 'wb')
* @return bool
*/
private function write_file(string $path, string $data, string $mode = 'wb'): bool
{
if ( ! $fp = @fopen($path, $mode))
return false;
flock($fp, LOCK_EX);
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
if (($result = fwrite($fp, substr($data, $written))) === false)
break;
flock($fp, LOCK_UN);
fclose($fp);
return is_int($result);
}
}

View File

@ -0,0 +1,56 @@
<?php
/**
* FuzeWorks ObjectStorage Component.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
namespace FuzeWorks\ObjectStorage;
use Traversable;
interface iObjectStorageProvider
{
public function init(array $providerConfig): bool;
public function getIndex(): array;
public function getItem(string $key): ?array;
public function getItemMeta(string $key): ?array;
public function getItems(array $keys = []): array;
public function hasItem(string $key): bool;
public function clear(): bool;
public function deleteItem(string $key): bool;
public function deleteItems(array $keys): bool;
public function save(string $key, $value, array $metaData = []): bool;
public function commit(): bool;
public function revert(): bool;
}

57
test/bootstrap.php Normal file
View File

@ -0,0 +1,57 @@
<?php
/**
* FuzeWorks ObjectStorage Component.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (c) 2013 - 2020, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
require_once(dirname(__DIR__) . '/vendor/autoload.php');
$configurator = new \FuzeWorks\Configurator();
// Set directories
$configurator->setTempDirectory(__DIR__ . '/temp');
$configurator->setLogDirectory(__DIR__ . '/temp');
// Other values
$configurator->setTimeZone('Europe/Amsterdam');
// Add TracyComponent
\FuzeWorks\TracyComponent::enableTracy();
$configurator->addComponent(new \FuzeWorks\TracyComponent());
// And the star of the show: ObjectStorageComponent
$configurator->addComponent(new \FuzeWorks\ObjectStorage\ObjectStorageComponent());
// Debug related
$configurator->enableDebugMode();
$configurator->setDebugAddress('ALL');
return $configurator->createContainer();

0
test/temp/placeholder Normal file
View File