From 085820d78cc9ae4631ed61468b522a1c53982a4c Mon Sep 17 00:00:00 2001 From: Origami11 Date: Fri, 26 Mar 2021 16:54:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D1=81=D1=82=D1=83=D0=BF=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/Component.php | 21 ++++-- src/Database.php | 7 +- src/Database/PDOStatement.php | 3 + src/Tools/SQLStatementExtractor.php | 2 +- src/Tools/String.php | 100 ++++++++++++++-------------- 5 files changed, 71 insertions(+), 62 deletions(-) diff --git a/src/Controller/Component.php b/src/Controller/Component.php index d77392e..e962a92 100644 --- a/src/Controller/Component.php +++ b/src/Controller/Component.php @@ -57,6 +57,7 @@ class Controller_Component public /*.Settings.*/$registry; public /*.Database.*/$db; public /*.Collection.*/$parameter; + static public $ID; public $module; public $item_module; @@ -223,7 +224,7 @@ class Controller_Component if (file_exists($path)) { require_once ($path); - $component = new $className(); + $component = new $className(); $component->db = $db; $component->registry = $registry; @@ -244,13 +245,18 @@ class Controller_Component $component->webPath = array(COMPONENTS_WEB . '/' . $name, SITE_WWW_PATH . '/components/' . $name); $component->COMPONENTS_WEB = COMPONENTS_WEB; } - } + } - $stmt = $db->prepareStatement("SELECT * FROM component WHERE code = ?"); - $stmt->setString(1, $expression); - $cid = $stmt->executeQuery(); - if ($cid->next()) { - $component->component_id = $cid->getInt('id_component'); + // Нужно для отличия компонентов на странице нужно извлекать id для всей страницы сразу? + // Просто вести глобальный счетчик для компонентов + + $component->component_id = self::$ID++; + /* + $stmt = $db->prepare("SELECT * FROM component WHERE code = :code"); + $stmt->execute(['code' => $expression]); + $cid = $stmt->fetch(); + if ($cid) { + $component->component_id = $cid['id_component']; } else { $last = $db->getIdGenerator(); if ($last->isBeforeInsert()) { @@ -269,6 +275,7 @@ class Controller_Component } $component->component_id = $result; } + */ $params = new Collection(); $params->import(array_merge($_GET, $arguments)); diff --git a/src/Database.php b/src/Database.php index 6dc72e9..98a09f9 100644 --- a/src/Database.php +++ b/src/Database.php @@ -35,8 +35,8 @@ class Database extends PDO static function getConnection(array $dsn) { - if ($dsn['phptype'] == 'pgsql' || $dsn['phptype'] == 'mysql') { - $port = (isset($dsn['port'])) ? "port={$dsn['port']};" : ""; + if ($dsn['phptype'] == 'pgsql' || $dsn['phptype'] == 'mysql') { + $port = (isset($dsn['port'])) ? "port={$dsn['port']};" : ""; /*.Database.*/$connection = new static("{$dsn['phptype']}:host={$dsn['hostspec']}; $port dbname={$dsn['database']}", $dsn['username'], $dsn['password']); if ($dsn['phptype'] == 'pgsql') { $connection->query('SET client_encoding="UTF-8"'); @@ -100,8 +100,7 @@ class Database extends PDO return $sth->fetch(PDO::FETCH_ASSOC); } - private static function assignQuote($x, $y) - { + function assignQuote($x, $y) { return $x . "=" . $this->quote($y); } diff --git a/src/Database/PDOStatement.php b/src/Database/PDOStatement.php index 43c7564..9ed84e2 100644 --- a/src/Database/PDOStatement.php +++ b/src/Database/PDOStatement.php @@ -95,6 +95,9 @@ class Database_PDOStatement extends PDOStatement implements IteratorAggregate function execute($args = null) { $result = parent::execute($args); + if (class_exists('Log')) { + Log::add('execute', [$this->queryString, $args]); + } return $result; } diff --git a/src/Tools/SQLStatementExtractor.php b/src/Tools/SQLStatementExtractor.php index 739f6c7..ae8b1c1 100644 --- a/src/Tools/SQLStatementExtractor.php +++ b/src/Tools/SQLStatementExtractor.php @@ -143,7 +143,7 @@ class Tools_SQLStatementExtractor { trigger_error("substring(), Endindex out of bounds must be $startpos 0) { // already in sub-array? - $subarr[$in_subarr][] = $tok; - if ('}' === substr($tok, -1, 1)) { // check to see if we just added last component - $res[] = static::strToArray(implode(',', $subarr[$in_subarr])); - $in_subarr--; - } - } elseif ($tok{0} === '{') { // we're inside a new sub-array - if ('}' !== substr($tok, -1, 1)) { - $in_subarr++; - // if sub-array has more than one element - $subarr[$in_subarr] = array(); - $subarr[$in_subarr][] = $tok; - } else { - $res[] = static::strToArray($tok); - } - } else { // not sub-array - $val = trim($tok, '"'); // remove " (surrounding strings) - // perform type castng here? - $res[] = $val; - } - } - - return $res; + $str = substr($str, 1, -1); // remove { } + $res = array(); + + $subarr = array(); + $in_subarr = 0; + + $toks = explode(',', $str); + foreach($toks as $tok) { + if ($in_subarr > 0) { // already in sub-array? + $subarr[$in_subarr][] = $tok; + if ('}' === substr($tok, -1, 1)) { // check to see if we just added last component + $res[] = static::strToArray(implode(',', $subarr[$in_subarr])); + $in_subarr--; + } + } elseif ($tok[0] === '{') { // we're inside a new sub-array + if ('}' !== substr($tok, -1, 1)) { + $in_subarr++; + // if sub-array has more than one element + $subarr[$in_subarr] = array(); + $subarr[$in_subarr][] = $tok; + } else { + $res[] = static::strToArray($tok); + } + } else { // not sub-array + $val = trim($tok, '"'); // remove " (surrounding strings) + // perform type castng here? + $res[] = $val; + } + } + + return $res; } - + //Нормализация строк на русском static function normalizeRussian($str) { $result = preg_replace('/\s+/',' ', $str); if (is_string($result)) { - $result = trim($result); //Замена длинных пробелов на одинарные, пробелы по краям - $result = mb_strtolower($result); - $result = preg_replace('/ё/','е', $str); //е на ё + $result = trim($result); //Замена длинных пробелов на одинарные, пробелы по краям + $result = mb_strtolower($result); + $result = preg_replace('/ё/','е', $str); //е на ё } - return $result; + return $result; } - + //Проверка равенства двух строк на русском языке. static function equalRussianCheck($str1,$str2) { - return self::normalizeRussian($str1) == self::normalizeRussian($str2); + return self::normalizeRussian($str1) == self::normalizeRussian($str2); } - - + + /** * Попадает ли строка в список вариантов * input: $str="foo1" $variants="foo1|foo2|foo3" * output: true * input: $str="foo" $variants="foo1|foo2|foo3" * output: false -*/ +*/ static function compare_string_to_variants($str, $variants){ $variants_array = explode('|', $variants); $founded = false; @@ -69,15 +69,15 @@ class Tools_String { } return $founded; } - + static function mb_str_split($str) { return preg_split('~~u', $str, null, PREG_SPLIT_NO_EMPTY); } - + static function mb_strtr($str, $from, $to) { return str_replace(self::mb_str_split($from), self::mb_str_split($to), $str); - } - + } + static function encodestring($st) { $st = self::mb_strtr($st,"абвгдеёзийклмнопрстуфхъыэ !+-()", "abvgdeeziyklmnoprstufh_ie______"); $st = self::mb_strtr($st,"АБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЭ", "ABVGDEEZIYKLMNOPRSTUFH_IE"); @@ -95,16 +95,16 @@ class Tools_String { "*" => '_', "ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh", "щ"=>"shch","ь"=>"", "ю"=>"yu", "я"=>"ya", - "Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH", + "Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH", "Щ"=>"SHCH","Ь"=>"", "Ю"=>"YU", "Я"=>"YA", "Й"=>"i", "й"=>"ie", "ё"=>"Ye", "№"=>"N" )); return strtolower($st); } - + static function validate_encoded_string($st) { $enc_st = self::encodestring($st); return preg_match('/^[\w_-]+(\.[\w_-]+)?$/', $enc_st); } -} +} \ No newline at end of file