CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • App
  • BasePlugin
  • ClassLoader
  • Configure
  • ObjectRegistry
  • Plugin
  • PluginCollection

Interfaces

  • ConsoleApplicationInterface
  • HttpApplicationInterface
  • PluginApplicationInterface
  • PluginInterface

Traits

  • ConventionsTrait
  • InstanceConfigTrait
  • StaticConfigTrait
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakephp.org CakePHP(tm) Project
 12:  * @since         3.0.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Core;
 16: 
 17: use BadMethodCallException;
 18: use InvalidArgumentException;
 19: use LogicException;
 20: 
 21: /**
 22:  * A trait that provides a set of static methods to manage configuration
 23:  * for classes that provide an adapter facade or need to have sets of
 24:  * configuration data registered and manipulated.
 25:  *
 26:  * Implementing objects are expected to declare a static `$_dsnClassMap` property.
 27:  */
 28: trait StaticConfigTrait
 29: {
 30:     /**
 31:      * Configuration sets.
 32:      *
 33:      * @var array
 34:      */
 35:     protected static $_config = [];
 36: 
 37:     /**
 38:      * This method can be used to define configuration adapters for an application.
 39:      *
 40:      * To change an adapter's configuration at runtime, first drop the adapter and then
 41:      * reconfigure it.
 42:      *
 43:      * Adapters will not be constructed until the first operation is done.
 44:      *
 45:      * ### Usage
 46:      *
 47:      * Assuming that the class' name is `Cache` the following scenarios
 48:      * are supported:
 49:      *
 50:      * Setting a cache engine up.
 51:      *
 52:      * ```
 53:      * Cache::setConfig('default', $settings);
 54:      * ```
 55:      *
 56:      * Injecting a constructed adapter in:
 57:      *
 58:      * ```
 59:      * Cache::setConfig('default', $instance);
 60:      * ```
 61:      *
 62:      * Configure multiple adapters at once:
 63:      *
 64:      * ```
 65:      * Cache::setConfig($arrayOfConfig);
 66:      * ```
 67:      *
 68:      * @param string|array $key The name of the configuration, or an array of multiple configs.
 69:      * @param array $config An array of name => configuration data for adapter.
 70:      * @throws \BadMethodCallException When trying to modify an existing config.
 71:      * @throws \LogicException When trying to store an invalid structured config array.
 72:      * @return void
 73:      */
 74:     public static function setConfig($key, $config = null)
 75:     {
 76:         if ($config === null) {
 77:             if (!is_array($key)) {
 78:                 throw new LogicException('If config is null, key must be an array.');
 79:             }
 80:             foreach ($key as $name => $settings) {
 81:                 static::setConfig($name, $settings);
 82:             }
 83: 
 84:             return;
 85:         }
 86: 
 87:         if (isset(static::$_config[$key])) {
 88:             throw new BadMethodCallException(sprintf('Cannot reconfigure existing key "%s"', $key));
 89:         }
 90: 
 91:         if (is_object($config)) {
 92:             $config = ['className' => $config];
 93:         }
 94: 
 95:         if (isset($config['url'])) {
 96:             $parsed = static::parseDsn($config['url']);
 97:             unset($config['url']);
 98:             $config = $parsed + $config;
 99:         }
100: 
101:         if (isset($config['engine']) && empty($config['className'])) {
102:             $config['className'] = $config['engine'];
103:             unset($config['engine']);
104:         }
105:         static::$_config[$key] = $config;
106:     }
107: 
108:     /**
109:      * Reads existing configuration.
110:      *
111:      * @param string $key The name of the configuration.
112:      * @return mixed Configuration data at the named key or null if the key does not exist.
113:      */
114:     public static function getConfig($key)
115:     {
116:         return isset(static::$_config[$key]) ? static::$_config[$key] : null;
117:     }
118: 
119:     /**
120:      * This method can be used to define configuration adapters for an application
121:      * or read existing configuration.
122:      *
123:      * To change an adapter's configuration at runtime, first drop the adapter and then
124:      * reconfigure it.
125:      *
126:      * Adapters will not be constructed until the first operation is done.
127:      *
128:      * ### Usage
129:      *
130:      * Assuming that the class' name is `Cache` the following scenarios
131:      * are supported:
132:      *
133:      * Reading config data back:
134:      *
135:      * ```
136:      * Cache::config('default');
137:      * ```
138:      *
139:      * Setting a cache engine up.
140:      *
141:      * ```
142:      * Cache::config('default', $settings);
143:      * ```
144:      *
145:      * Injecting a constructed adapter in:
146:      *
147:      * ```
148:      * Cache::config('default', $instance);
149:      * ```
150:      *
151:      * Configure multiple adapters at once:
152:      *
153:      * ```
154:      * Cache::config($arrayOfConfig);
155:      * ```
156:      *
157:      * @deprecated 3.4.0 Use setConfig()/getConfig() instead.
158:      * @param string|array $key The name of the configuration, or an array of multiple configs.
159:      * @param array|null $config An array of name => configuration data for adapter.
160:      * @return array|null Null when adding configuration or an array of configuration data when reading.
161:      * @throws \BadMethodCallException When trying to modify an existing config.
162:      */
163:     public static function config($key, $config = null)
164:     {
165:         deprecationWarning(
166:             get_called_class() . '::config() is deprecated. ' .
167:             'Use setConfig()/getConfig() instead.'
168:         );
169: 
170:         if ($config !== null || is_array($key)) {
171:             static::setConfig($key, $config);
172: 
173:             return null;
174:         }
175: 
176:         return static::getConfig($key);
177:     }
178: 
179:     /**
180:      * Drops a constructed adapter.
181:      *
182:      * If you wish to modify an existing configuration, you should drop it,
183:      * change configuration and then re-add it.
184:      *
185:      * If the implementing objects supports a `$_registry` object the named configuration
186:      * will also be unloaded from the registry.
187:      *
188:      * @param string $config An existing configuration you wish to remove.
189:      * @return bool Success of the removal, returns false when the config does not exist.
190:      */
191:     public static function drop($config)
192:     {
193:         if (!isset(static::$_config[$config])) {
194:             return false;
195:         }
196:         if (isset(static::$_registry)) {
197:             static::$_registry->unload($config);
198:         }
199:         unset(static::$_config[$config]);
200: 
201:         return true;
202:     }
203: 
204:     /**
205:      * Returns an array containing the named configurations
206:      *
207:      * @return string[] Array of configurations.
208:      */
209:     public static function configured()
210:     {
211:         return array_keys(static::$_config);
212:     }
213: 
214:     /**
215:      * Parses a DSN into a valid connection configuration
216:      *
217:      * This method allows setting a DSN using formatting similar to that used by PEAR::DB.
218:      * The following is an example of its usage:
219:      *
220:      * ```
221:      * $dsn = 'mysql://user:pass@localhost/database?';
222:      * $config = ConnectionManager::parseDsn($dsn);
223:      *
224:      * $dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS';
225:      * $config = Log::parseDsn($dsn);
226:      *
227:      * $dsn = 'smtp://user:secret@localhost:25?timeout=30&client=null&tls=null';
228:      * $config = Email::parseDsn($dsn);
229:      *
230:      * $dsn = 'file:///?className=\My\Cache\Engine\FileEngine';
231:      * $config = Cache::parseDsn($dsn);
232:      *
233:      * $dsn = 'File://?prefix=myapp_cake_core_&serialize=true&duration=+2 minutes&path=/tmp/persistent/';
234:      * $config = Cache::parseDsn($dsn);
235:      * ```
236:      *
237:      * For all classes, the value of `scheme` is set as the value of both the `className`
238:      * unless they have been otherwise specified.
239:      *
240:      * Note that querystring arguments are also parsed and set as values in the returned configuration.
241:      *
242:      * @param string $dsn The DSN string to convert to a configuration array
243:      * @return array The configuration array to be stored after parsing the DSN
244:      * @throws \InvalidArgumentException If not passed a string, or passed an invalid string
245:      */
246:     public static function parseDsn($dsn)
247:     {
248:         if (empty($dsn)) {
249:             return [];
250:         }
251: 
252:         if (!is_string($dsn)) {
253:             throw new InvalidArgumentException('Only strings can be passed to parseDsn');
254:         }
255: 
256:         $pattern = <<<'REGEXP'
257: {
258:     ^
259:     (?P<_scheme>
260:         (?P<scheme>[\w\\\\]+)://
261:     )
262:     (?P<_username>
263:         (?P<username>.*?)
264:         (?P<_password>
265:             :(?P<password>.*?)
266:         )?
267:         @
268:     )?
269:     (?P<_host>
270:         (?P<host>[^?#/:@]+)
271:         (?P<_port>
272:             :(?P<port>\d+)
273:         )?
274:     )?
275:     (?P<_path>
276:         (?P<path>/[^?#]*)
277:     )?
278:     (?P<_query>
279:         \?(?P<query>[^#]*)
280:     )?
281:     (?P<_fragment>
282:         \#(?P<fragment>.*)
283:     )?
284:     $
285: }x
286: REGEXP;
287: 
288:         preg_match($pattern, $dsn, $parsed);
289: 
290:         if (!$parsed) {
291:             throw new InvalidArgumentException("The DSN string '{$dsn}' could not be parsed.");
292:         }
293: 
294:         $exists = [];
295:         foreach ($parsed as $k => $v) {
296:             if (is_int($k)) {
297:                 unset($parsed[$k]);
298:             } elseif (strpos($k, '_') === 0) {
299:                 $exists[substr($k, 1)] = ($v !== '');
300:                 unset($parsed[$k]);
301:             } elseif ($v === '' && !$exists[$k]) {
302:                 unset($parsed[$k]);
303:             }
304:         }
305: 
306:         $query = '';
307: 
308:         if (isset($parsed['query'])) {
309:             $query = $parsed['query'];
310:             unset($parsed['query']);
311:         }
312: 
313:         parse_str($query, $queryArgs);
314: 
315:         foreach ($queryArgs as $key => $value) {
316:             if ($value === 'true') {
317:                 $queryArgs[$key] = true;
318:             } elseif ($value === 'false') {
319:                 $queryArgs[$key] = false;
320:             } elseif ($value === 'null') {
321:                 $queryArgs[$key] = null;
322:             }
323:         }
324: 
325:         $parsed = $queryArgs + $parsed;
326: 
327:         if (empty($parsed['className'])) {
328:             $classMap = static::getDsnClassMap();
329: 
330:             $parsed['className'] = $parsed['scheme'];
331:             if (isset($classMap[$parsed['scheme']])) {
332:                 $parsed['className'] = $classMap[$parsed['scheme']];
333:             }
334:         }
335: 
336:         return $parsed;
337:     }
338: 
339:     /**
340:      * Updates the DSN class map for this class.
341:      *
342:      * @param array $map Additions/edits to the class map to apply.
343:      * @return void
344:      */
345:     public static function setDsnClassMap(array $map)
346:     {
347:         static::$_dsnClassMap = $map + static::$_dsnClassMap;
348:     }
349: 
350:     /**
351:      * Returns the DSN class map for this class.
352:      *
353:      * @return array
354:      */
355:     public static function getDsnClassMap()
356:     {
357:         return static::$_dsnClassMap;
358:     }
359: 
360:     /**
361:      * Returns or updates the DSN class map for this class.
362:      *
363:      * @deprecated 3.4.0 Use setDsnClassMap()/getDsnClassMap() instead.
364:      * @param array|null $map Additions/edits to the class map to apply.
365:      * @return array
366:      */
367:     public static function dsnClassMap(array $map = null)
368:     {
369:         deprecationWarning(
370:             get_called_class() . '::setDsnClassMap() is deprecated. ' .
371:             'Use setDsnClassMap()/getDsnClassMap() instead.'
372:         );
373: 
374:         if ($map !== null) {
375:             static::setDsnClassMap($map);
376:         }
377: 
378:         return static::getDsnClassMap();
379:     }
380: }
381: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs