CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • BakeTask
  • ControllerTask
  • DbConfigTask
  • ExtractTask
  • FixtureTask
  • ModelTask
  • PluginTask
  • ProjectTask
  • TemplateTask
  • TestTask
  • ViewTask
  1: <?php
  2: /**
  3:  * The DbConfig Task handles creating and updating the database.php
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @since         CakePHP(tm) v 1.2
 16:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 17:  */
 18: 
 19: App::uses('AppShell', 'Console/Command');
 20: 
 21: /**
 22:  * Task class for creating and updating the database configuration file.
 23:  *
 24:  * @package       Cake.Console.Command.Task
 25:  */
 26: class DbConfigTask extends AppShell {
 27: 
 28: /**
 29:  * path to CONFIG directory
 30:  *
 31:  * @var string
 32:  */
 33:     public $path = null;
 34: 
 35: /**
 36:  * Default configuration settings to use
 37:  *
 38:  * @var array
 39:  */
 40:     protected $_defaultConfig = array(
 41:         'name' => 'default',
 42:         'datasource' => 'Database/Mysql',
 43:         'persistent' => 'false',
 44:         'host' => 'localhost',
 45:         'login' => 'root',
 46:         'password' => 'password',
 47:         'database' => 'project_name',
 48:         'schema' => null,
 49:         'prefix' => null,
 50:         'encoding' => null,
 51:         'port' => null
 52:     );
 53: 
 54: /**
 55:  * String name of the database config class name.
 56:  * Used for testing.
 57:  *
 58:  * @var string
 59:  */
 60:     public $databaseClassName = 'DATABASE_CONFIG';
 61: 
 62: /**
 63:  * initialization callback
 64:  *
 65:  * @return void
 66:  */
 67:     public function initialize() {
 68:         $this->path = APP . 'Config' . DS;
 69:     }
 70: 
 71: /**
 72:  * Execution method always used for tasks
 73:  *
 74:  * @return void
 75:  */
 76:     public function execute() {
 77:         if (empty($this->args)) {
 78:             $this->_interactive();
 79:             $this->_stop();
 80:         }
 81:     }
 82: 
 83: /**
 84:  * Interactive interface
 85:  *
 86:  * @return void
 87:  */
 88:     protected function _interactive() {
 89:         $this->hr();
 90:         $this->out(__d('cake_console', 'Database Configuration:'));
 91:         $this->hr();
 92:         $done = false;
 93:         $dbConfigs = array();
 94: 
 95:         while ($done == false) {
 96:             $name = '';
 97: 
 98:             while ($name == '') {
 99:                 $name = $this->in(__d('cake_console', "Name:"), null, 'default');
100:                 if (preg_match('/[^a-z0-9_]/i', $name)) {
101:                     $name = '';
102:                     $this->out(__d('cake_console', 'The name may only contain unaccented latin characters, numbers or underscores'));
103:                 } elseif (preg_match('/^[^a-z_]/i', $name)) {
104:                     $name = '';
105:                     $this->out(__d('cake_console', 'The name must start with an unaccented latin character or an underscore'));
106:                 }
107:             }
108: 
109:             $datasource = $this->in(__d('cake_console', 'Datasource:'), array('Mysql', 'Postgres', 'Sqlite', 'Sqlserver'), 'Mysql');
110: 
111:             $persistent = $this->in(__d('cake_console', 'Persistent Connection?'), array('y', 'n'), 'n');
112:             if (strtolower($persistent) == 'n') {
113:                 $persistent = 'false';
114:             } else {
115:                 $persistent = 'true';
116:             }
117: 
118:             $host = '';
119:             while ($host == '') {
120:                 $host = $this->in(__d('cake_console', 'Database Host:'), null, 'localhost');
121:             }
122: 
123:             $port = '';
124:             while ($port == '') {
125:                 $port = $this->in(__d('cake_console', 'Port?'), null, 'n');
126:             }
127: 
128:             if (strtolower($port) == 'n') {
129:                 $port = null;
130:             }
131: 
132:             $login = '';
133:             while ($login == '') {
134:                 $login = $this->in(__d('cake_console', 'User:'), null, 'root');
135:             }
136:             $password = '';
137:             $blankPassword = false;
138: 
139:             while ($password == '' && $blankPassword == false) {
140:                 $password = $this->in(__d('cake_console', 'Password:'));
141: 
142:                 if ($password == '') {
143:                     $blank = $this->in(__d('cake_console', 'The password you supplied was empty. Use an empty password?'), array('y', 'n'), 'n');
144:                     if ($blank == 'y') {
145:                         $blankPassword = true;
146:                     }
147:                 }
148:             }
149: 
150:             $database = '';
151:             while ($database == '') {
152:                 $database = $this->in(__d('cake_console', 'Database Name:'), null, 'cake');
153:             }
154: 
155:             $prefix = '';
156:             while ($prefix == '') {
157:                 $prefix = $this->in(__d('cake_console', 'Table Prefix?'), null, 'n');
158:             }
159:             if (strtolower($prefix) == 'n') {
160:                 $prefix = null;
161:             }
162: 
163:             $encoding = '';
164:             while ($encoding == '') {
165:                 $encoding = $this->in(__d('cake_console', 'Table encoding?'), null, 'n');
166:             }
167:             if (strtolower($encoding) == 'n') {
168:                 $encoding = null;
169:             }
170: 
171:             $schema = '';
172:             if ($datasource == 'postgres') {
173:                 while ($schema == '') {
174:                     $schema = $this->in(__d('cake_console', 'Table schema?'), null, 'n');
175:                 }
176:             }
177:             if (strtolower($schema) == 'n') {
178:                 $schema = null;
179:             }
180: 
181:             $config = compact('name', 'datasource', 'persistent', 'host', 'login', 'password', 'database', 'prefix', 'encoding', 'port', 'schema');
182: 
183:             while ($this->_verify($config) == false) {
184:                 $this->_interactive();
185:             }
186: 
187:             $dbConfigs[] = $config;
188:             $doneYet = $this->in(__d('cake_console', 'Do you wish to add another database configuration?'), null, 'n');
189: 
190:             if (strtolower($doneYet == 'n')) {
191:                 $done = true;
192:             }
193:         }
194: 
195:         $this->bake($dbConfigs);
196:         config('database');
197:         return true;
198:     }
199: 
200: /**
201:  * Output verification message and bake if it looks good
202:  *
203:  * @param array $config
204:  * @return boolean True if user says it looks good, false otherwise
205:  */
206:     protected function _verify($config) {
207:         $config = array_merge($this->_defaultConfig, $config);
208:         extract($config);
209:         $this->out();
210:         $this->hr();
211:         $this->out(__d('cake_console', 'The following database configuration will be created:'));
212:         $this->hr();
213:         $this->out(__d('cake_console', "Name:         %s", $name));
214:         $this->out(__d('cake_console', "Datasource:       %s", $datasource));
215:         $this->out(__d('cake_console', "Persistent:   %s", $persistent));
216:         $this->out(__d('cake_console', "Host:         %s", $host));
217: 
218:         if ($port) {
219:             $this->out(__d('cake_console', "Port:         %s", $port));
220:         }
221: 
222:         $this->out(__d('cake_console', "User:         %s", $login));
223:         $this->out(__d('cake_console', "Pass:         %s", str_repeat('*', strlen($password))));
224:         $this->out(__d('cake_console', "Database:     %s", $database));
225: 
226:         if ($prefix) {
227:             $this->out(__d('cake_console', "Table prefix: %s", $prefix));
228:         }
229: 
230:         if ($schema) {
231:             $this->out(__d('cake_console', "Schema:       %s", $schema));
232:         }
233: 
234:         if ($encoding) {
235:             $this->out(__d('cake_console', "Encoding:     %s", $encoding));
236:         }
237: 
238:         $this->hr();
239:         $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n'), 'y');
240: 
241:         if (strtolower($looksGood) == 'y') {
242:             return $config;
243:         }
244:         return false;
245:     }
246: 
247: /**
248:  * Assembles and writes database.php
249:  *
250:  * @param array $configs Configuration settings to use
251:  * @return boolean Success
252:  */
253:     public function bake($configs) {
254:         if (!is_dir($this->path)) {
255:             $this->err(__d('cake_console', '%s not found', $this->path));
256:             return false;
257:         }
258: 
259:         $filename = $this->path . 'database.php';
260:         $oldConfigs = array();
261: 
262:         if (file_exists($filename)) {
263:             config('database');
264:             $db = new $this->databaseClassName;
265:             $temp = get_class_vars(get_class($db));
266: 
267:             foreach ($temp as $configName => $info) {
268:                 $info = array_merge($this->_defaultConfig, $info);
269: 
270:                 if (!isset($info['schema'])) {
271:                     $info['schema'] = null;
272:                 }
273:                 if (!isset($info['encoding'])) {
274:                     $info['encoding'] = null;
275:                 }
276:                 if (!isset($info['port'])) {
277:                     $info['port'] = null;
278:                 }
279: 
280:                 if ($info['persistent'] === false) {
281:                     $info['persistent'] = 'false';
282:                 } else {
283:                     $info['persistent'] = ($info['persistent'] == true) ? 'true' : 'false';
284:                 }
285: 
286:                 $oldConfigs[] = array(
287:                     'name' => $configName,
288:                     'datasource' => $info['datasource'],
289:                     'persistent' => $info['persistent'],
290:                     'host' => $info['host'],
291:                     'port' => $info['port'],
292:                     'login' => $info['login'],
293:                     'password' => $info['password'],
294:                     'database' => $info['database'],
295:                     'prefix' => $info['prefix'],
296:                     'schema' => $info['schema'],
297:                     'encoding' => $info['encoding']
298:                 );
299:             }
300:         }
301: 
302:         foreach ($oldConfigs as $key => $oldConfig) {
303:             foreach ($configs as $k => $config) {
304:                 if ($oldConfig['name'] == $config['name']) {
305:                     unset($oldConfigs[$key]);
306:                 }
307:             }
308:         }
309: 
310:         $configs = array_merge($oldConfigs, $configs);
311:         $out = "<?php\n";
312:         $out .= "class DATABASE_CONFIG {\n\n";
313: 
314:         foreach ($configs as $config) {
315:             $config = array_merge($this->_defaultConfig, $config);
316:             extract($config);
317: 
318:             $out .= "\tpublic \${$name} = array(\n";
319:             $out .= "\t\t'datasource' => 'Database/{$datasource}',\n";
320:             $out .= "\t\t'persistent' => {$persistent},\n";
321:             $out .= "\t\t'host' => '{$host}',\n";
322: 
323:             if ($port) {
324:                 $out .= "\t\t'port' => {$port},\n";
325:             }
326: 
327:             $out .= "\t\t'login' => '{$login}',\n";
328:             $out .= "\t\t'password' => '{$password}',\n";
329:             $out .= "\t\t'database' => '{$database}',\n";
330: 
331:             if ($schema) {
332:                 $out .= "\t\t'schema' => '{$schema}',\n";
333:             }
334: 
335:             if ($prefix) {
336:                 $out .= "\t\t'prefix' => '{$prefix}',\n";
337:             }
338: 
339:             if ($encoding) {
340:                 $out .= "\t\t'encoding' => '{$encoding}'\n";
341:             }
342: 
343:             $out .= "\t);\n";
344:         }
345: 
346:         $out .= "}\n";
347:         $filename = $this->path . 'database.php';
348:         return $this->createFile($filename, $out);
349:     }
350: 
351: /**
352:  * Get a user specified Connection name
353:  *
354:  * @return void
355:  */
356:     public function getConfig() {
357:         App::uses('ConnectionManager', 'Model');
358:         $configs = ConnectionManager::enumConnectionObjects();
359: 
360:         $useDbConfig = key($configs);
361:         if (!is_array($configs) || empty($configs)) {
362:             return $this->execute();
363:         }
364:         $connections = array_keys($configs);
365: 
366:         if (count($connections) > 1) {
367:             $useDbConfig = $this->in(__d('cake_console', 'Use Database Config') . ':', $connections, $useDbConfig);
368:         }
369:         return $useDbConfig;
370:     }
371: 
372: /**
373:  * get the option parser
374:  *
375:  * @return ConsoleOptionParser
376:  */
377:     public function getOptionParser() {
378:         $parser = parent::getOptionParser();
379:         return $parser->description(
380:                 __d('cake_console', 'Bake new database configuration settings.')
381:             );
382:     }
383: 
384: }
385: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs