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

  • IniConfig
  • JsonConfig
  • PhpConfig
  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\Configure\Engine;
 16: 
 17: use Cake\Core\Configure\ConfigEngineInterface;
 18: use Cake\Core\Configure\FileConfigTrait;
 19: use Cake\Core\Exception\Exception;
 20: 
 21: /**
 22:  * JSON engine allows Configure to load configuration values from
 23:  * files containing JSON strings.
 24:  *
 25:  * An example JSON file would look like::
 26:  *
 27:  * ```
 28:  * {
 29:  *     "debug": false,
 30:  *     "App": {
 31:  *         "namespace": "MyApp"
 32:  *     },
 33:  *     "Security": {
 34:  *         "salt": "its-secret"
 35:  *     }
 36:  * }
 37:  * ```
 38:  */
 39: class JsonConfig implements ConfigEngineInterface
 40: {
 41:     use FileConfigTrait;
 42: 
 43:     /**
 44:      * File extension.
 45:      *
 46:      * @var string
 47:      */
 48:     protected $_extension = '.json';
 49: 
 50:     /**
 51:      * Constructor for JSON Config file reading.
 52:      *
 53:      * @param string|null $path The path to read config files from. Defaults to CONFIG.
 54:      */
 55:     public function __construct($path = null)
 56:     {
 57:         if ($path === null) {
 58:             $path = CONFIG;
 59:         }
 60:         $this->_path = $path;
 61:     }
 62: 
 63:     /**
 64:      * Read a config file and return its contents.
 65:      *
 66:      * Files with `.` in the name will be treated as values in plugins. Instead of
 67:      * reading from the initialized path, plugin keys will be located using Plugin::path().
 68:      *
 69:      * @param string $key The identifier to read from. If the key has a . it will be treated
 70:      *   as a plugin prefix.
 71:      * @return array Parsed configuration values.
 72:      * @throws \Cake\Core\Exception\Exception When files don't exist or when
 73:      *   files contain '..' (as this could lead to abusive reads) or when there
 74:      *   is an error parsing the JSON string.
 75:      */
 76:     public function read($key)
 77:     {
 78:         $file = $this->_getFilePath($key, true);
 79: 
 80:         $values = json_decode(file_get_contents($file), true);
 81:         if (json_last_error() !== JSON_ERROR_NONE) {
 82:             throw new Exception(sprintf(
 83:                 'Error parsing JSON string fetched from config file "%s.json": %s',
 84:                 $key,
 85:                 json_last_error_msg()
 86:             ));
 87:         }
 88:         if (!is_array($values)) {
 89:             throw new Exception(sprintf(
 90:                 'Decoding JSON config file "%s.json" did not return an array',
 91:                 $key
 92:             ));
 93:         }
 94: 
 95:         return $values;
 96:     }
 97: 
 98:     /**
 99:      * Converts the provided $data into a JSON string that can be used saved
100:      * into a file and loaded later.
101:      *
102:      * @param string $key The identifier to write to. If the key has a . it will
103:      *  be treated as a plugin prefix.
104:      * @param array $data Data to dump.
105:      * @return bool Success
106:      */
107:     public function dump($key, array $data)
108:     {
109:         $filename = $this->_getFilePath($key);
110: 
111:         return file_put_contents($filename, json_encode($data, JSON_PRETTY_PRINT)) > 0;
112:     }
113: }
114: 
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