Exemplo n.º 1
0
 public static function createConnection(DI\Container $container)
 {
     $dibiConnection = new \DibiConnection($container->params['database']);
     $dibiConnection->query('SET NAMES UTF8');
     $substitutions = array('core' => 'cms_', 'vd' => 'cms_vd_', 'c' => 'cgf_', 'media' => 'media_');
     foreach ($substitutions as $sub => $prefix) {
         $dibiConnection->getSubstitutes()->{$sub} = $prefix;
     }
     //        $profiler = new \DibiProfiler();
     //        $dibiConnection->setProfiler($profiler);
     //        $dibiConnection->setFile(APP_DIR.'/../log/dibi.log');
     return $dibiConnection;
 }
 /**
  * PREG callback from translate() or formatValue().
  * @param  array
  * @return string
  */
 private function cb($matches)
 {
     //    [1] => `ident`
     //    [2] => [ident]
     //    [3] => '
     //    [4] => string
     //    [5] => "
     //    [6] => string
     //    [7] => lone-quote
     //    [8] => substitution
     //    [9] => substitution flag
     //    [10] => modifier (when called from self::translate())
     //    [11] => placeholder (when called from self::translate())
     if (!empty($matches[11])) {
         // placeholder
         $cursor =& $this->cursor;
         if ($cursor >= count($this->args)) {
             $this->hasError = TRUE;
             return "**Extra placeholder**";
         }
         $cursor++;
         return $this->formatValue($this->args[$cursor - 1], FALSE);
     }
     if (!empty($matches[10])) {
         // modifier
         $mod = $matches[10];
         $cursor =& $this->cursor;
         if ($cursor >= count($this->args) && $mod !== 'else' && $mod !== 'end') {
             $this->hasError = TRUE;
             return "**Extra modifier %{$mod}**";
         }
         if ($mod === 'if') {
             $this->ifLevel++;
             $cursor++;
             if (!$this->comment && !$this->args[$cursor - 1]) {
                 // open comment
                 $this->ifLevelStart = $this->ifLevel;
                 $this->comment = TRUE;
                 return "/*";
             }
             return '';
         } elseif ($mod === 'else') {
             if ($this->ifLevelStart === $this->ifLevel) {
                 $this->ifLevelStart = 0;
                 $this->comment = FALSE;
                 return "*/";
             } elseif (!$this->comment) {
                 $this->ifLevelStart = $this->ifLevel;
                 $this->comment = TRUE;
                 return "/*";
             }
         } elseif ($mod === 'end') {
             $this->ifLevel--;
             if ($this->ifLevelStart === $this->ifLevel + 1) {
                 // close comment
                 $this->ifLevelStart = 0;
                 $this->comment = FALSE;
                 return "*/";
             }
             return '';
         } elseif ($mod === 'ex') {
             // array expansion
             array_splice($this->args, $cursor, 1, $this->args[$cursor]);
             return '';
         } elseif ($mod === 'lmt') {
             // apply limit
             if ($this->args[$cursor] !== NULL) {
                 $this->limit = (int) $this->args[$cursor];
             }
             $cursor++;
             return '';
         } elseif ($mod === 'ofs') {
             // apply offset
             if ($this->args[$cursor] !== NULL) {
                 $this->offset = (int) $this->args[$cursor];
             }
             $cursor++;
             return '';
         } else {
             // default processing
             $cursor++;
             return $this->formatValue($this->args[$cursor - 1], $mod);
         }
     }
     if ($this->comment) {
         return '...';
     }
     if ($matches[1]) {
         // SQL identifiers: `ident`
         return $this->identifiers->{$matches[1]};
     }
     if ($matches[2]) {
         // SQL identifiers: [ident]
         return $this->identifiers->{$matches[2]};
     }
     if ($matches[3]) {
         // SQL strings: '...'
         return $this->driver->escape(str_replace("''", "'", $matches[4]), dibi::TEXT);
     }
     if ($matches[5]) {
         // SQL strings: "..."
         return $this->driver->escape(str_replace('""', '"', $matches[6]), dibi::TEXT);
     }
     if ($matches[7]) {
         // string quote
         $this->hasError = TRUE;
         return '**Alone quote**';
     }
     if ($matches[8]) {
         // SQL identifier substitution
         $m = substr($matches[8], 0, -1);
         $m = $this->connection->getSubstitutes()->{$m};
         return $matches[9] == '' ? $this->formatValue($m, FALSE) : $m . $matches[9];
         // value or identifier
     }
     die('this should be never executed');
 }