web/lib/Zend/Tool/Project/Provider/DbAdapter.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Tool
       
    17  * @subpackage Framework
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: DbAdapter.php 20852 2010-02-02 21:59:18Z ralph $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Tool_Project_Provider_Abstract
       
    25  */
       
    26 require_once 'Zend/Tool/Project/Provider/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Tool_Framework_Provider_Interactable
       
    30  */
       
    31 require_once 'Zend/Tool/Framework/Provider/Interactable.php';
       
    32 
       
    33 /**
       
    34  * @category   Zend
       
    35  * @package    Zend_Tool
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_Tool_Project_Provider_DbAdapter
       
    40     extends Zend_Tool_Project_Provider_Abstract
       
    41     implements Zend_Tool_Framework_Provider_Interactable, Zend_Tool_Framework_Provider_Pretendable
       
    42 {
       
    43     
       
    44     protected $_appConfigFilePath = null;
       
    45     
       
    46     protected $_config = null;
       
    47     
       
    48     protected $_sectionName = 'production';
       
    49     
       
    50     public function configure($dsn = null, /* $interactivelyPrompt = false, */ $sectionName = 'production')
       
    51     {
       
    52         $profile = $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
       
    53         
       
    54         $appConfigFileResource = $profile->search('applicationConfigFile');
       
    55                 
       
    56         if ($appConfigFileResource == false) {
       
    57             throw new Zend_Tool_Project_Exception('A project with an application config file is required to use this provider.');
       
    58         }
       
    59         
       
    60         $this->_appConfigFilePath = $appConfigFileResource->getPath();
       
    61         
       
    62         $this->_config = new Zend_Config_Ini($this->_appConfigFilePath, null, array('skipExtends' => true, 'allowModifications' => true));
       
    63         
       
    64         if ($sectionName != 'production') {
       
    65             $this->_sectionName = $sectionName;
       
    66         }
       
    67         
       
    68         if (!isset($this->_config->{$this->_sectionName})) {
       
    69             throw new Zend_Tool_Project_Exception('The config does not have a ' . $this->_sectionName . ' section.');
       
    70         }
       
    71         
       
    72         if (isset($this->_config->{$this->_sectionName}->resources->db)) {
       
    73             throw new Zend_Tool_Project_Exception('The config already has a db resource configured in section ' . $this->_sectionName . '.');
       
    74         }
       
    75         
       
    76         if ($dsn) {
       
    77             $this->_configureViaDSN($dsn);
       
    78         //} elseif ($interactivelyPrompt) {
       
    79         //    $this->_promptForConfig();
       
    80         } else {
       
    81             $this->_registry->getResponse()->appendContent('Nothing to do!');
       
    82         }
       
    83         
       
    84         
       
    85     }
       
    86     
       
    87     protected function _configureViaDSN($dsn)
       
    88     {
       
    89         $dsnVars = array();
       
    90         
       
    91         if (strpos($dsn, '=') === false) {
       
    92             throw new Zend_Tool_Project_Provider_Exception('At least one name value pair is expected, typcially '
       
    93                 . 'in the format of "adapter=Mysqli&username=uname&password=mypass&dbname=mydb"' 
       
    94                 );
       
    95         }
       
    96         
       
    97         parse_str($dsn, $dsnVars);
       
    98 
       
    99         // parse_str suffers when magic_quotes is enabled
       
   100         if (get_magic_quotes_gpc()) {
       
   101             array_walk_recursive($dsnVars, array($this, '_cleanMagicQuotesInValues'));
       
   102         }
       
   103         
       
   104         $dbConfigValues = array('resources' => array('db' => null));
       
   105         
       
   106         if (isset($dsnVars['adapter'])) {
       
   107             $dbConfigValues['resources']['db']['adapter'] = $dsnVars['adapter'];
       
   108             unset($dsnVars['adapter']);
       
   109         }
       
   110         
       
   111         $dbConfigValues['resources']['db']['params'] = $dsnVars;
       
   112         
       
   113         $isPretend = $this->_registry->getRequest()->isPretend();
       
   114 
       
   115         // get the config resource
       
   116         $applicationConfig = $this->_loadedProfile->search('ApplicationConfigFile');
       
   117         $applicationConfig->addItem($dbConfigValues, $this->_sectionName, null);
       
   118         
       
   119         $response = $this->_registry->getResponse();
       
   120         
       
   121         if ($isPretend) {
       
   122             $response->appendContent('A db configuration for the ' . $this->_sectionName
       
   123                 . ' section would be written to the application config file with the following contents: '
       
   124                 );
       
   125             $response->appendContent($applicationConfig->getContents());
       
   126         } else {
       
   127             $applicationConfig->create();
       
   128             $response->appendContent('A db configuration for the ' . $this->_sectionName
       
   129                 . ' section has been written to the application config file.'
       
   130                 );
       
   131         }
       
   132     }
       
   133     
       
   134     protected function _cleanMagicQuotesInValues(&$value, $key)
       
   135     {
       
   136         $value = stripslashes($value);
       
   137     }
       
   138     
       
   139 }