flipbox /
cache
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @copyright Copyright (c) Flipbox Digital Limited |
||
| 5 | * @license https://github.com/flipbox/cache/blob/master/LICENSE |
||
| 6 | * @link https://github.com/flipbox/cache |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace Flipbox\Cache; |
||
| 10 | |||
| 11 | use Flipbox\Cache\Exceptions\InvalidDriverException; |
||
| 12 | use Flipbox\Skeleton\Helpers\ArrayHelper; |
||
| 13 | use Flipbox\Skeleton\Helpers\ObjectHelper; |
||
| 14 | use Flipbox\Skeleton\Logger\AutoLoggerTrait; |
||
| 15 | use Psr\Log\LoggerInterface; |
||
| 16 | use Stash\Driver\BlackHole as DummyDriver; |
||
| 17 | use Stash\Interfaces\DriverInterface; |
||
| 18 | use Stash\Interfaces\PoolInterface; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @author Flipbox Factory <[email protected]> |
||
| 22 | * @since 2.0.0 |
||
| 23 | */ |
||
| 24 | class Factory |
||
| 25 | { |
||
| 26 | |||
| 27 | use AutoLoggerTrait; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * This is the default time, in seconds, that objects are cached for. |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | public $defaultDuration = 432000; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The default driver |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $defaultDriver = 'file'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Registered cache drivers |
||
| 45 | * |
||
| 46 | * @var DriverInterface[] |
||
| 47 | */ |
||
| 48 | protected $registeredDrivers = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Constructor |
||
| 52 | * |
||
| 53 | * @param LoggerInterface $logger |
||
| 54 | */ |
||
| 55 | public function __construct(LoggerInterface $logger) |
||
| 56 | { |
||
| 57 | $this->setLogger($logger); |
||
| 58 | |||
| 59 | // Manually register the default driver |
||
| 60 | $this->registeredDrivers['dummy'] = new DummyDriver(); |
||
| 61 | } |
||
| 62 | |||
| 63 | /******************************************* |
||
| 64 | * CREATE |
||
| 65 | *******************************************/ |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Create a new cache pool |
||
| 69 | * |
||
| 70 | * @param array $config |
||
| 71 | * @return PoolInterface |
||
| 72 | * @throws InvalidDriverException |
||
| 73 | */ |
||
| 74 | public function create($config = []): PoolInterface |
||
| 75 | { |
||
| 76 | // Array |
||
| 77 | $config = ArrayHelper::toArray($config); |
||
| 78 | |||
| 79 | // Get driver from config |
||
| 80 | if (!$driverType = ObjectHelper::findClassFromConfig($config)) { |
||
| 81 | $driverType = $this->defaultDriver; |
||
| 82 | |||
| 83 | $this->warning( |
||
| 84 | "Cache pool configuration did not indicate a driver type...using default", |
||
| 85 | [ |
||
| 86 | 'config' => $config, |
||
| 87 | 'default' => $driverType |
||
| 88 | ] |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** @var Pool $pool */ |
||
| 93 | $pool = new Pool( |
||
| 94 | $this->autoGetDriver($driverType) |
||
| 95 | ); |
||
| 96 | |||
| 97 | // Set logger |
||
| 98 | $pool->setLogger($this->getLogger()); |
||
|
0 ignored issues
–
show
|
|||
| 99 | |||
| 100 | // Set duration |
||
| 101 | $pool->setItemDuration(ArrayHelper::getValue( |
||
| 102 | $config, |
||
| 103 | 'duration', |
||
| 104 | $this->defaultDuration |
||
| 105 | )); |
||
| 106 | |||
| 107 | return $pool; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get a cache driver. If the driver is not found, return the default. |
||
| 112 | * |
||
| 113 | * @param $identifier |
||
| 114 | * @return DriverInterface |
||
| 115 | * @throws InvalidDriverException |
||
| 116 | */ |
||
| 117 | public function autoGetDriver($identifier): DriverInterface |
||
| 118 | { |
||
| 119 | if (!$driver = ArrayHelper::getValue($this->registeredDrivers, $identifier)) { |
||
| 120 | $this->critical( |
||
| 121 | "Cache driver not available...switching to default", |
||
| 122 | [ |
||
| 123 | 'driver' => $identifier, |
||
| 124 | 'default' => $this->defaultDriver, |
||
| 125 | 'registered' => array_keys( |
||
| 126 | $this->registeredDrivers |
||
| 127 | ) |
||
| 128 | ] |
||
| 129 | ); |
||
| 130 | |||
| 131 | // Get default driver |
||
| 132 | $driver = $this->getDriver($this->defaultDriver); |
||
| 133 | } |
||
| 134 | return $driver; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get a cache driver |
||
| 139 | * |
||
| 140 | * @param $identifier |
||
| 141 | * @return DriverInterface |
||
| 142 | * @throws InvalidDriverException |
||
| 143 | */ |
||
| 144 | public function getDriver($identifier): DriverInterface |
||
| 145 | { |
||
| 146 | if (!$driver = ArrayHelper::getValue($this->registeredDrivers, $identifier)) { |
||
| 147 | throw new InvalidDriverException(sprintf( |
||
| 148 | "Driver type '%s' is not registered.", |
||
| 149 | $identifier |
||
| 150 | )); |
||
| 151 | } |
||
| 152 | return $driver; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Register a cache driver for use |
||
| 157 | * |
||
| 158 | * @param $identifier |
||
| 159 | * @param DriverInterface $driver |
||
| 160 | * @throws InvalidDriverException |
||
| 161 | */ |
||
| 162 | public function registerDriver($identifier, DriverInterface $driver) |
||
| 163 | { |
||
| 164 | // Handle already taken |
||
| 165 | if (ArrayHelper::keyExists($identifier, $this->registeredDrivers)) { |
||
| 166 | throw new InvalidDriverException(sprintf( |
||
| 167 | "Driver type '%s' is already registered.", |
||
| 168 | $identifier |
||
| 169 | )); |
||
| 170 | } |
||
| 171 | |||
| 172 | $this->info( |
||
| 173 | "Registered cache driver", |
||
| 174 | [ |
||
| 175 | 'driver' => $identifier, |
||
| 176 | 'class' => get_class($driver) |
||
| 177 | ] |
||
| 178 | ); |
||
| 179 | |||
| 180 | $this->registeredDrivers[$identifier] = $driver; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Register an array of drivers |
||
| 185 | * |
||
| 186 | * @param DriverInterface[] $drivers |
||
| 187 | * @throws InvalidDriverException |
||
| 188 | */ |
||
| 189 | public function registerDrivers(array $drivers) |
||
| 190 | { |
||
| 191 | foreach ($drivers as $handle => $driver) { |
||
| 192 | $this->registerDriver($handle, $driver); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get an array of registered cache drivers |
||
| 198 | * |
||
| 199 | * @return DriverInterface[] |
||
| 200 | */ |
||
| 201 | public function getDrivers(): array |
||
| 202 | { |
||
| 203 | return $this->registeredDrivers; |
||
| 204 | } |
||
| 205 | } |
||
| 206 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: