-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputHelper.php
72 lines (62 loc) · 1.65 KB
/
OutputHelper.php
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
<?php
declare(strict_types=1);
/*
* This file is part of BiuradPHP opensource projects.
*
* PHP version 7.1 and above required
*
* @author Divine Niiquaye Ibok <divineibok@gmail.com>
* @copyright 2019 Biurad Group (https://biurad.com/)
* @license https://opensource.org/licenses/BSD-3-Clause License
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Biurad\Cache;
use Biurad\Cache\Exceptions\InvalidArgumentException;
use Psr\Cache\CacheItemInterface;
/**
* Output caching helper.
*/
class OutputHelper
{
/** @var null|FastCache */
private $cache;
/** @var string */
private $key;
/**
* @param FastCache $cache
* @param mixed $key
*/
public function __construct(FastCache $cache, $key)
{
$this->cache = $cache;
$this->key = $key;
\ob_start();
}
/**
* Stops and saves the cache.
*
* @param callable $callback
* @param null|float $beta
*
* @throws InvalidArgumentException
*/
public function end(callable $callback = null, ?float $beta = null): void
{
if (null === $this->cache) {
throw new InvalidArgumentException('Output cache has already been saved.');
}
$this->cache->save(
$this->key,
function (CacheItemInterface $item, bool $save) use ($callback) {
if (null !== $callback) {
$callback(...[&$item, &$save]);
}
return \ob_get_flush();
},
$beta
);
$this->cache = null;
}
}