Overview

Namespaces

  • Charcoal
    • App
      • Action
      • Config
      • Handler
      • Middleware
      • Module
      • Route
      • Script
      • ServiceProvider
      • Template

Classes

  • Charcoal\App\Action\AbstractAction
  • Charcoal\App\App
  • Charcoal\App\AppConfig
  • Charcoal\App\AppContainer
  • Charcoal\App\Config\CacheConfig
  • Charcoal\App\Config\DatabaseConfig
  • Charcoal\App\Config\FilesystemConfig
  • Charcoal\App\Config\LoggerConfig
  • Charcoal\App\Config\MemcacheCacheConfig
  • Charcoal\App\Config\MemcacheCacheServerConfig
  • Charcoal\App\Handler\AbstractHandler
  • Charcoal\App\Handler\Error
  • Charcoal\App\Handler\HandlerConfig
  • Charcoal\App\Handler\NotAllowed
  • Charcoal\App\Handler\NotFound
  • Charcoal\App\Handler\PhpError
  • Charcoal\App\Handler\Shutdown
  • Charcoal\App\Middleware\CacheMiddleware
  • Charcoal\App\Module\AbstractModule
  • Charcoal\App\Module\ModuleConfig
  • Charcoal\App\Module\ModuleManager
  • Charcoal\App\Route\ActionRoute
  • Charcoal\App\Route\ActionRouteConfig
  • Charcoal\App\Route\RouteConfig
  • Charcoal\App\Route\RouteManager
  • Charcoal\App\Route\ScriptRoute
  • Charcoal\App\Route\ScriptRouteConfig
  • Charcoal\App\Route\TemplateRoute
  • Charcoal\App\Route\TemplateRouteConfig
  • Charcoal\App\Script\AbstractScript
  • Charcoal\App\ServiceProvider\AppServiceProvider
  • Charcoal\App\ServiceProvider\CacheServiceProvider
  • Charcoal\App\ServiceProvider\DatabaseServiceProvider
  • Charcoal\App\ServiceProvider\FilesystemServiceProvider
  • Charcoal\App\ServiceProvider\LoggerServiceProvider
  • Charcoal\App\ServiceProvider\ViewServiceProvider
  • Charcoal\App\Template\AbstractTemplate
  • Charcoal\App\Template\AbstractWidget
  • Charcoal\App\Template\WidgetBuilder

Interfaces

  • Charcoal\App\Action\ActionInterface
  • Charcoal\App\AppAwareInterface
  • Charcoal\App\Handler\HandlerInterface
  • Charcoal\App\Module\ModuleInterface
  • Charcoal\App\Route\RouteInterface
  • Charcoal\App\Script\CronScriptInterface
  • Charcoal\App\Script\ScriptInterface
  • Charcoal\App\Template\TemplateInterface
  • Charcoal\App\Template\WidgetInterface

Traits

  • Charcoal\App\AppAwareTrait
  • Charcoal\App\CallableResolverAwareTrait
  • Charcoal\App\Script\ArgScriptTrait
  • Charcoal\App\Script\CronScriptTrait
  • Charcoal\App\Script\PathScriptTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\App\Config;
  4: 
  5: // Dependencies from `PHP`
  6: use InvalidArgumentException;
  7: 
  8: // Module `charcoal-config` dependencies
  9: use Charcoal\Config\AbstractConfig;
 10: 
 11: /**
 12:  * Cache Configuration
 13:  */
 14: class CacheConfig extends AbstractConfig
 15: {
 16:     /**
 17:      * @var array
 18:      */
 19:     private $types = ['memory'];
 20: 
 21:     /**
 22:      * The default TTL, in seconds.
 23:      * 10 days by default.
 24:      *
 25:      * @var integer $defaultTtl
 26:      */
 27:     private $defaultTtl = 864000;
 28: 
 29:     /**
 30:      * @var string $prefix
 31:      */
 32:     private $prefix = 'charcoal';
 33: 
 34:     /**
 35:      * @var array
 36:      */
 37:     public $middleware;
 38: 
 39:     /**
 40:      * @return array
 41:      */
 42:     public function defaults()
 43:     {
 44:         return [
 45:             'types'         => ['memory'],
 46:             'default_ttl'   => 864000,
 47:             'prefix'        => 'charcoal',
 48:             'middleware'    => $this->middlewareDefaults()
 49:         ];
 50:     }
 51: 
 52:     /**
 53:      * @return array
 54:      */
 55:     private function middlewareDefaults()
 56:     {
 57:         return [
 58:             'active'         => true,
 59:             'included_path'  => null,
 60:             'excluded_path'  => null,
 61:             'methods'        => [
 62:                 'GET'
 63:             ],
 64:             'status_codes'   => [
 65:                 200
 66:             ],
 67:             'ttl'            => 0,
 68:             'included_query' => null,
 69:             'excluded_query' => null,
 70:             'ignored_query'  => null
 71:         ];
 72:     }
 73: 
 74:     /**
 75:      * Set the types (drivers) of cache.
 76:      *
 77:      * The first cache actually available on the system will be the one used for caching.
 78:      *
 79:      * @param string[] $types The types of cache to try using, in order of priority.
 80:      * @return CacheConfig Chainable
 81:      */
 82:     public function setTypes(array $types)
 83:     {
 84:         $this->types = [];
 85:         foreach ($types as $type) {
 86:             $this->addType($type);
 87:         }
 88:         return $this;
 89:     }
 90: 
 91:     /**
 92:      * Get the valid types (drivers).
 93:      *
 94:      * @return array
 95:      */
 96:     public function validTypes()
 97:     {
 98:         return [
 99:             'apc',
100:             'file',
101:             'db',
102:             'memcache',
103:             'memory',
104:             'noop',
105:             'redis'
106:         ];
107:     }
108: 
109:     /**
110:      * @param string $type The cache type.
111:      * @throws InvalidArgumentException If the type is not a string.
112:      * @return CacheConfig Chainable
113:      */
114:     public function addType($type)
115:     {
116:         if (!in_array($type, $this->validTypes())) {
117:             throw new InvalidArgumentException(
118:                 sprintf('Invalid cache type: "%s"', $type)
119:             );
120:         }
121:         $this->types[] = $type;
122:         return $this;
123:     }
124: 
125:     /**
126:      * @return array
127:      */
128:     public function types()
129:     {
130:         return $this->types;
131:     }
132: 
133:     /**
134:      * @param integer $ttl The time-to-live, in seconds.
135:      * @throws InvalidArgumentException If the TTL argument is not numeric.
136:      * @return CacheConfig Chainable
137:      */
138:     public function setDefaultTtl($ttl)
139:     {
140:         if (!is_numeric($ttl)) {
141:             throw new InvalidArgumentException(
142:                 'TTL must be an integer (seconds).'
143:             );
144:         }
145:         $this->defaultTtl = (int)$ttl;
146:         return $this;
147:     }
148: 
149:     /**
150:      * @return integer
151:      */
152:     public function defaultTtl()
153:     {
154:         return $this->defaultTtl;
155:     }
156: 
157:     /**
158:      * @param string $prefix The cache prefix (or namespace).
159:      * @throws InvalidArgumentException If the prefix is not a string.
160:      * @return CacheConfig Chainable
161:      */
162:     public function setPrefix($prefix)
163:     {
164:         if (!is_string($prefix)) {
165:             throw new InvalidArgumentException(
166:                 'Prefix must be a string.'
167:             );
168:         }
169:         $this->prefix = $prefix;
170:         return $this;
171:     }
172: 
173:     /**
174:      * @return string
175:      */
176:     public function prefix()
177:     {
178:         return $this->prefix;
179:     }
180: 
181:     /**
182:      * @param array $middleware The cache middleware configuration.
183:      * @return CacheConfig Chainable
184:      */
185:     public function setMiddleware(array $middleware)
186:     {
187:         $middleware = array_merge($this->middlewareDefaults(), $middleware);
188:         $this->middleware = $middleware;
189:         return $this;
190:     }
191: 
192:     /**
193:      * @return array
194:      */
195:     public function middleware()
196:     {
197:         return $this->middleware;
198:     }
199: }
200: 
API documentation generated by ApiGen