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
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie;
use ICanBoogie\Storage\APCStorage;
use ICanBoogie\Storage\FileStorage;
use ICanBoogie\Storage\Storage;
use ICanBoogie\Storage\StorageCollection;
class Hooks
{
/**
* If APC is available the method return a storage collection with a {@link APCStorage}
* instance and the specified storage instance.
*
* @param Storage $storage
* @param string $prefix Prefix for the {@link APCStorage} instance.
*
* @return Storage|StorageCollection
*/
static private function with_apc_storage(Storage $storage, $prefix)
{
if (!APCStorage::is_available())
{
return $storage;
}
return new StorageCollection([ new APCStorage(self::make_apc_prefix() . $prefix), $storage ]);
}
/**
* Makes an APC prefix for the application.
*
* @return string
*/
static public function make_apc_prefix()
{
return substr(sha1(ROOT), 0, 8) . ':';
}
/**
* Creates a storage engine for synthesized configurations.
*
* If APC is available the method returns a storage collection or {@link APCStorage} and
* {@link FileStorage}, otherwise a {@link FileStorage} is returned.
*
* @param Core $app
*
* @return Storage
*/
static public function create_storage_for_configs(Core $app)
{
$storage = new FileStorage(REPOSITORY . 'cache' . DIRECTORY_SEPARATOR . 'configs');
return self::with_apc_storage($storage, 'icanboogie:configs:');
}
/**
* Creates a storage engine for synthesized configurations.
*
* If APC is available the method returns a storage collection or {@link APCStorage} and
* {@link FileStorage}, otherwise a {@link FileStorage} is returned.
*
* @param Core $app
*
* @return Storage
*/
static public function create_storage_for_vars(Core $app)
{
$storage = new FileStorage(REPOSITORY . 'vars');
return self::with_apc_storage($storage, 'icanboogie:vars:');
}
/*
* Events
*/
/**
* Clears configurations cache.
*
* @param Core\ClearCacheEvent $event
* @param Core $app
*/
static public function on_clear_cache(Core\ClearCacheEvent $event, Core $app)
{
$app->storage_for_configs->clear();
}
}