public function runByParameter($invokeParams = null)
 {
     if (is_null($invokeParams)) {
         return false;
     }
     if (!EhrlichAndreas_Util_Object::isInstanceOf($invokeParams, 'EhrlichAndreas_Mvc_Request')) {
         $invokeParams = EhrlichAndreas_Util_Array::objectToArray($invokeParams);
         $request = new EhrlichAndreas_Mvc_Request();
         $this->getRouter()->setRequestParams($request, $invokeParams);
     } else {
         $request = $invokeParams;
     }
     $class = implode('_', array(ucfirst($request->getModuleName()), ucfirst($request->getSubmoduleName()), ucfirst($request->getControllerName()) . 'Controller'));
     $action = $request->getActionName();
     $controller = new $class($request);
     $controller->setView($this->getView());
     return $controller->dispatch($action);
 }
Exemplo n.º 2
0
 /**
  * Factory for EhrlichAndreas_Db_Adapter_Abstract classes.
  *
  * First argument may be a string containing the base of the adapter class
  * name, e.g. 'Mysqli' corresponds to class EhrlichAndreas_Db_Adapter_Mysqli. This
  * name is currently case-insensitive, but is not ideal to rely on this
  * behavior.
  * If your class is named 'EhrlichAndreas_Db_Adapter_Pdo_Mysql', where
  * 'EhrlichAndreas_Db_Adapter' is the
  * namespace
  * and 'Pdo_Mysql' is the adapter name, it is best to use the name exactly
  * as it
  * is defined in the class. This will ensure proper use of the factory API.
  *
  * First argument may alternatively be array.
  * The adapter class base name is read from the 'adapter' property.
  * The adapter config parameters are read from the 'params' property.
  *
  * Second argument is optional and may be an associative array of key-value
  * pairs. This is used as the argument to the adapter constructor.
  *
  * If the first argument is of type Iterator, it is assumed to contain
  * all parameters, and the second argument is ignored.
  *
  * @param mixed $adapter
  *            String name of base adapter class, or array.
  * @param mixed $config
  *            OPTIONAL; an array or array with adapter
  *            parameters.
  * @return EhrlichAndreas_Db_Adapter_Abstract
  * @throws EhrlichAndreas_Db_Exception
  */
 public static function factory($adapter, $config = array())
 {
     if (is_object($config) && method_exists($config, 'toArray')) {
         $config = $config->toArray();
     }
     if (is_object($adapter) && method_exists($adapter, 'toArray')) {
         $adapter = $adapter->toArray();
     }
     /*
      * Convert array argument to plain string adapter name and separate
      * config object.
      */
     if (is_array($adapter)) {
         if (isset($adapter['params'])) {
             $config = $adapter['params'];
         }
         if (empty($config)) {
             $config = $adapter;
         }
         if (isset($adapter['adapter'])) {
             $adapter = (string) $adapter['adapter'];
         } else {
             $adapter = null;
         }
     }
     /*
      * Verify that adapter parameters are in an array.
      */
     if (!is_array($config)) {
         /**
          *
          * @see EhrlichAndreas_Db_Exception
          */
         throw new EhrlichAndreas_Db_Exception('Adapter parameters must be in an array or a Iterator object');
     }
     if (isset($config['dsn'])) {
         $dsn = EhrlichAndreas_Util_Dsn::parseDsn($config['dsn']);
         $uri = EhrlichAndreas_Util_Dsn::parseUri($config['dsn']);
         $driver_name = $uri[0];
         $config = $config + $dsn;
         /*
          * Verify that an adapter name has been specified.
          */
         if ((!is_string($adapter) || empty($adapter)) && isset($config['driver']) && stripos($config['driver'], '_') === false) {
             $adapter = $config['driver'] . '_' . $driver_name;
         }
     }
     /*
      * Verify that an adapter name has been specified.
      */
     if (!is_string($adapter) || empty($adapter)) {
         /**
          *
          * @see EhrlichAndreas_Db_Exception
          */
         throw new EhrlichAndreas_Db_Exception('Adapter name must be specified in a string');
     }
     /*
      * Form full adapter class name
      */
     $adapterNamespace = 'EhrlichAndreas_Db_Adapter';
     if (isset($config['adapterNamespace'])) {
         if ($config['adapterNamespace'] != '') {
             $adapterNamespace = $config['adapterNamespace'];
         }
         unset($config['adapterNamespace']);
     }
     $adapterName = $adapterNamespace . '_';
     $adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));
     /*
      * Load the adapter class. This throws an exception if the specified
      * class cannot be loaded.
      */
     if (!class_exists($adapterName)) {
         throw new EhrlichAndreas_Db_Exception("Adapter class '{$adapterName}' does not exist");
     }
     $notInstanceOf = true;
     $notInstanceOf = $notInstanceOf && !EhrlichAndreas_Util_Object::isInstanceOf($adapterName, 'EhrlichAndreas_Db_Adapter_Abstract');
     $notInstanceOf = $notInstanceOf && !EhrlichAndreas_Util_Object::isInstanceOf($adapterName, 'Zend_Db_Adapter_Abstract');
     $notInstanceOf = $notInstanceOf && !EhrlichAndreas_Util_Object::isInstanceOf($adapterName, 'MiniPhp_Db_Adapter_Abstract');
     /*
      * Verify that the object created is a descendent of the abstract
      * adapter type.
      */
     if ($notInstanceOf) {
         /**
          *
          * @see EhrlichAndreas_Db_Exception
          */
         throw new EhrlichAndreas_Db_Exception("Adapter class '{$adapterName}' does not extend EhrlichAndreas_Db_Adapter_Abstract");
     }
     /*
      * Create an instance of the adapter class. Pass the config to the
      * adapter class constructor.
      */
     $adapter = str_replace(' ', '_', ucwords(str_replace('_', ' ', $adapter)));
     $config['adapter'] = $adapter;
     $dbAdapter = new $adapterName($config);
     return $dbAdapter;
 }
 /**
  * Return the connection resource
  * If we are not connected, the connection is made
  *
  * @throws EhrlichAndreas_AbstractCms_Exception
  * @return EhrlichAndreas_Db_Adapter_Abstract Connection resource
  */
 public function getConnection()
 {
     if (!empty($this->db) || !empty($this->options['db'])) {
         if (EhrlichAndreas_Util_Object::isInstanceOf($this->db, 'EhrlichAndreas_Db_Adapter_Abstract') || EhrlichAndreas_Util_Object::isInstanceOf($this->db, 'Zend_Db_Adapter_Abstract')) {
             return $this->db;
         } else {
             $this->db = $this->options['db'];
             if (!EhrlichAndreas_Util_Object::isInstanceOf($this->db, 'EhrlichAndreas_Db_Adapter_Abstract') && !EhrlichAndreas_Util_Object::isInstanceOf($this->db, 'Zend_Db_Adapter_Abstract')) {
                 $this->throwException("Impossible to open " . $this->options['db'] . " DB", $this->options['exceptionclass']);
             }
             return $this->db;
         }
     } elseif (!empty($this->options['dbconfig'])) {
         $adapter = EhrlichAndreas_Db_Db::getAdapterName($this->options['dbconfig']);
         if (!EhrlichAndreas_Util_Object::isInstanceOf($adapter, 'EhrlichAndreas_Db_Adapter_Abstract') && !EhrlichAndreas_Util_Object::isInstanceOf($this->db, 'Zend_Db_Adapter_Abstract')) {
             $this->throwException("Impossible to open " . $adapter . " DB", $this->options['exceptionclass']);
         }
         $dbconfig = $this->options['dbconfig'];
         if (isset($dbconfig['dbtableprefix'])) {
             unset($dbconfig['dbtableprefix']);
         }
         if (isset($dbconfig['dbtableprefixlength'])) {
             unset($dbconfig['dbtableprefixlength']);
         }
         if (isset($dbconfig['dbtableprefixlengthmax'])) {
             unset($dbconfig['dbtableprefixlengthmax']);
         }
         if (isset($dbconfig['dbtableprefixoffset'])) {
             unset($dbconfig['dbtableprefixoffset']);
         }
         if (isset($dbconfig['dbtablesuffix'])) {
             unset($dbconfig['dbtablesuffix']);
         }
         if (isset($dbconfig['dbtablesuffixlength'])) {
             unset($dbconfig['dbtablesuffixlength']);
         }
         if (isset($dbconfig['dbtablesuffixlengthmax'])) {
             unset($dbconfig['dbtablesuffixlengthmax']);
         }
         if (isset($dbconfig['dbtablesuffixoffset'])) {
             unset($dbconfig['dbtablesuffixoffset']);
         }
         $this->db = EhrlichAndreas_Db_Db::factory($dbconfig);
         return $this->db;
     } else {
         $this->throwException("Impossible to open DB", $this->options['exceptionclass']);
     }
 }
 /**
  * Fetches all result rows as a sequential array.
  *
  * @param EhrlichAndreas_AbstractCms_Abstract_Adapter_Abstract|EhrlichAndreas_Db_Adapter_Abstract|Zend_Db_Adapter_Abstract $adapter
  * @param string $table
  * @param string|array $where
  *            OPTIONAL An SQL WHERE clause.
  * @param string|array $cols
  *            OPTIONAL The columns to select from the joined table.
  * @param string|array $order
  *            OPTIONAL The column(s) and direction to order by.
  * @param string|array $group
  *            OPTIONAL The column(s) to group by.
  * @param string|array $having
  *            OPTIONAL The HAVING condition.
  * @param int $count
  *            OPTIONAL An SQL LIMIT count.
  * @param int $offset
  *            OPTIONAL An SQL LIMIT offset.
  * @param string $returnAsString
  *            Return the computed query as string.
  * @return array The row results per assoc array fetch mode.
  * @throws Exception
  */
 public function fetchAll($adapter, $table, $where = null, $cols = null, $order = null, $group = null, $having = null, $count = null, $offset = null, $returnAsString = false)
 {
     $db = EhrlichAndreas_AbstractCms_Abstract_Model::getConnection($adapter);
     if (!EhrlichAndreas_Util_Object::isInstanceOf($where, 'EhrlichAndreas_Db_Select')) {
         $query = new EhrlichAndreas_Db_Select($db);
         $cols = $cols == null ? '*' : $cols;
         if (!is_array($cols)) {
             $cols = array($cols);
         }
         foreach ($cols as $key => $value) {
             $escape = true;
             if (is_array($value)) {
                 if (array_key_exists('escape', $value) && !is_null($value['escape'])) {
                     $escape = $value['escape'];
                 } elseif (count($value) == 2 && array_key_exists(0, $value) && array_key_exists(1, $value) && !is_null($value[1]) && is_bool($value[1])) {
                     $escape = $value[1];
                 }
                 if (array_key_exists('value', $value)) {
                     $value = $value['value'];
                 } elseif (array_key_exists(0, $value)) {
                     $value = $value[0];
                 } else {
                     $value = array_shift($value);
                 }
             } elseif (is_object($value) && method_exists($value, '__toString')) {
                 $value = $value->__toString();
                 $escape = false;
             }
             $escape = (bool) intval($escape);
             if (!$escape) {
                 $value = new EhrlichAndreas_Db_Expr($value);
             }
             $cols[$key] = $value;
         }
         $query->from($table, $cols);
         if ($where !== null) {
             $where = (array) $where;
             foreach ($where as $key => $value) {
                 $escape = true;
                 if (is_array($value)) {
                     if (array_key_exists('escape', $value) && !is_null($value['escape'])) {
                         $escape = $value['escape'];
                     } elseif (count($value) == 2 && array_key_exists(0, $value) && array_key_exists(1, $value) && !is_null($value[1]) && is_bool($value[1])) {
                         $escape = $value[1];
                     }
                     if (array_key_exists('value', $value)) {
                         $value = $value['value'];
                     } elseif (count($value) == 2 && array_key_exists(0, $value) && array_key_exists(1, $value) && !is_null($value[1]) && is_bool($value[1])) {
                         $value = $value[0];
                     } else {
                         //$value = array_shift($value);
                     }
                 } elseif (is_object($value) && method_exists($value, '__toString')) {
                     $value = $value->__toString();
                     $escape = false;
                 }
                 $escape = (bool) intval($escape);
                 if (!$escape) {
                     $value = new EhrlichAndreas_Db_Expr($value);
                 }
                 if (is_int($key)) {
                     // $value is the full condition
                     $query->where($value);
                 } elseif (!is_array($value)) {
                     // $key is the condition with placeholder,
                     // and $val is quoted into the condition
                     $query->where($db->quoteIdentifier($key) . ' = ?', $value);
                 } else {
                     // $key is the condition with placeholder,
                     // and $val is array quoted into the condition
                     $query->where($db->quoteIdentifier($key) . ' in (?)', $value);
                 }
             }
         }
         if ($order !== null) {
             if (is_string($order)) {
                 $order = explode(',', $order);
             }
             if (!is_array($order)) {
                 $order = array($order);
             }
             foreach ($order as $val) {
                 $query->order($val);
             }
         }
         if ($group !== null) {
             if (is_string($group)) {
                 $group = explode(',', $group);
             }
             if (!is_array($group)) {
                 $group = array($group);
             }
             foreach ($group as $val) {
                 $query->group($val);
             }
         }
         if ($having !== null) {
             if (!is_array($having)) {
                 $having = array($having);
             }
             foreach ($having as $val) {
                 $query->having($val);
             }
         }
         if ($count !== null || $offset !== null) {
             $query->limit($count, $offset);
         }
     } else {
         $query = $where;
     }
     $query = $query->assemble();
     if ($returnAsString) {
         return $query;
     }
     $fetchMode = $db->getFetchMode();
     $stmt = $db->query($query);
     $return = $stmt->fetchAll($fetchMode);
     $stmt->closeCursor();
     // $return = $db->fetchAll($query);
     return $return;
 }
 /**
  * Test if a connection is active
  *
  * @return boolean
  */
 public function isConnected()
 {
     return (bool) (EhrlichAndreas_Util_Object::isInstanceOf($this->_connection, 'EhrlichAndreas_Pdo_Pdo') || EhrlichAndreas_Util_Object::isInstanceOf($this->_connection, 'PDO'));
 }
 /**
  * 
  * @param mixed $options
  * @return array
  */
 protected function _getCmsConfigFromAdapter($options = array())
 {
     if (!is_array($options)) {
         if (EhrlichAndreas_Util_Object::isInstanceOf($options, 'Zend\\Db\\Adapter\\Adapter')) {
             $options = new EhrlichAndreas_Db_ZF2Bridge_Adapter($options);
         }
         if (EhrlichAndreas_Util_Object::isInstanceOf($options, 'Doctrine\\Bundle\\DoctrineBundle\\Registry')) {
             $options = new EhrlichAndreas_Db_DoctrineBridge_Adapter($options);
         }
         if (EhrlichAndreas_Util_Object::isInstanceOf($options, 'EhrlichAndreas_Db_Adapter_Abstract') || EhrlichAndreas_Util_Object::isInstanceOf($options, 'Zend_Db_Adapter_Abstract')) {
             $adapter = $options;
             $dbConfig = $options->getConfig();
             if (isset($dbConfig['dsn'])) {
                 $data = EhrlichAndreas_Util_Dsn::parseDsn($dbConfig['dsn']);
                 foreach ($data as $key => $value) {
                     $dbConfig[$key] = $value;
                 }
                 $data = EhrlichAndreas_Util_Dsn::parseUri($dbConfig['dsn']);
                 if (isset($data[0])) {
                     $dbConfig['adapter'] = ucfirst($data[0]);
                 }
                 if (isset($dbConfig['adapter']) && $dbConfig['driver']) {
                     $dbConfig['adapter'] = $dbConfig['driver'] . '_' . $dbConfig['adapter'];
                 }
             }
             $options = array('db' => $adapter, 'dbconfig' => $dbConfig, 'params' => $dbConfig);
             if (isset($dbConfig['adapter'])) {
                 $options['adapter'] = $dbConfig['adapter'];
             }
             if (isset($dbConfig['install'])) {
                 $options['install'] = $dbConfig['install'];
             }
         }
     }
     return $options;
 }
Exemplo n.º 7
0
 protected function _mapEhrlichAndreasToZend($param)
 {
     if ($this->_adapterIsEhrlichAndreas || !is_array($param) && !is_object($param)) {
         return $param;
     }
     if (is_array($param)) {
         $return = array();
         foreach ($param as $key => $value) {
             $key = $this->_mapEhrlichAndreasToZend($key);
             $value = $this->_mapEhrlichAndreasToZend($value);
             $return[$key] = $value;
         }
         $param = $return;
     }
     if (EhrlichAndreas_Util_Object::isInstanceOf($param, 'EhrlichAndreas_Db_Expr')) {
         $param = $param->__toString();
         $param = new Zend_Db_Expr($param);
     }
     if (EhrlichAndreas_Util_Object::isInstanceOf($param, 'EhrlichAndreas_Db_Select')) {
         $param = $param->assamble();
         $param = new EhrlichAndreas_Zend_Db_Select($param);
     }
     return $param;
 }