/** * @param mixed $a * @param mixed $b * @param bool $strong typing * @return mixed */ function debug_enforce_eq($a, $b, $strong = false) { return debug_enforce($strong ? $a === $b : $a == $b, var_dump_human_compact($a) . " doesn't equals to " . var_dump_human_compact($b)); }
/** * @param array|object $collection * @param callable $callable * @param bool $recursionFirst * @return array|object * @throws Exception */ function collection_map_val_recursive($collection, $callable, $recursionFirst = true) { if (is_array($collection)) { $ret = array_map_val_recursive($collection, function () use($callable, $recursionFirst) { $args = func_get_args(); if (!$recursionFirst) { $args[0] = call_user_func_array($callable, $args); } if (is_object($args[0]) || is_array($args[0])) { $args[0] = collection_map_val_recursive($args[0], $callable, $recursionFirst); } if ($recursionFirst) { $args[0] = call_user_func_array($callable, $args); } return $args[0]; }, $recursionFirst); } elseif (is_object($collection)) { $ret = object_map_val_recursive($collection, function () use($callable, $recursionFirst) { $args = func_get_args(); if (!$recursionFirst) { $args[0] = call_user_func_array($callable, $args); } if (is_array($args[0]) || is_object($args[0])) { $args[0] = collection_map_val_recursive($args[0], $callable, $recursionFirst); } if ($recursionFirst) { $args[0] = call_user_func_array($callable, $args); } return $args[0]; }, $recursionFirst); } else { debug_enforce(false, "Cannot handle collection: " . var_dump_human_compact($collection)); $ret = []; } return $ret; }
/** * WARNING: Only columns copying supported. * @param Lib_Model_Db_Mysql $model * @param string|null $thisKeyCol may contain table prefix or not * @param string|null $thatKeyCol may contain table prefix or not * @param string $conditions * @return $this */ public function joinFrom($model, $thisKeyCol, $thatKeyCol = null, $conditions = '') { if ($thatKeyCol === null) { debug_assert($thisKeyCol !== null); $thatKeyCol = $model->getPrimaryKey(); } elseif ($thisKeyCol === null) { debug_assert($thatKeyCol !== null); $thisKeyCol = $this->getPrimaryKey(); } $this->prefixColumn($model, $thatKeyCol); $this->prefixColumn($this, $thisKeyCol); $conditions = str_replace('{that}', $model->getAlias(), $conditions); $conditions = str_replace('{this}', $this->getAlias(), $conditions); $this->mapPartWhere($this->addAliasToConditionDg()); $model->mapPartWhere($model->addAliasToConditionDg()); $thisFrom = $this->_select->getPart(Zend_Db_Select::FROM); $modelColumns = array_chain($model->_select->getPart(Zend_Db_Select::COLUMNS), array_group_dg(array_get_dg(return_dg(0))), array_map_val_dg(array_chain_dg(array_map_val_dg(function ($descriptor) { return null === $descriptor[2] ? $descriptor[1] : [$descriptor[2] => $descriptor[1]]; }), function ($columns) { $outArray = []; array_map_val($columns, function ($column) use(&$outArray) { if (is_array($column)) { array_map_val($column, function ($column, $alias) use(&$outArray) { $outArray[$alias] = $column; }); } else { $outArray[] = $column; } }); return $outArray; }))); array_each($model->_select->getPart(Zend_Db_Select::FROM), function ($descriptor, $alias) use($modelColumns, $thisFrom, $model, $thisKeyCol, $thatKeyCol, $conditions) { debug_enforce(!array_key_exists($alias, $thisFrom), "Alias `{$alias}` already used for table `{$descriptor['tableName']}`"); switch ($descriptor['joinType']) { case Zend_Db_Select::FROM: $this->_select->joinLeft([$model->getAlias() => $model->getTable()], "{$thisKeyCol}={$thatKeyCol} " . $conditions, array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']); break; case Zend_Db_Select::INNER_JOIN: $this->_select->joinInner([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']); break; case Zend_Db_Select::LEFT_JOIN: $this->_select->joinLeft([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']); break; case Zend_Db_Select::RIGHT_JOIN: $this->_select->joinRight([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']); break; case Zend_Db_Select::FULL_JOIN: $this->_select->joinFull([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']); break; case Zend_Db_Select::CROSS_JOIN: $this->_select->joinCross([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']); break; case Zend_Db_Select::NATURAL_JOIN: $this->_select->joinNatural([$alias => $descriptor['tableName']], $descriptor['joinCondition'], array_key_exists($alias, $modelColumns) ? $modelColumns[$alias] : [], $descriptor['schema']); break; default: debug_assert(false, "Unknown join type " . var_dump_human_compact($descriptor['joinType'])); break; } }); $this->settingsJoin($model); return $this; }
/** * @param string $column * @return bool */ public function propertyExists($column) { debug_enforce(!empty($column), "Cannot check if empty property exists"); if ($column == 'id' && $this->getPrimaryKey()) { $column = $this->getPrimaryKey(); } // Watch out for this incompatible change return $this->schemaColumnExists($column); }
/** * @param array $commands * @param callable $onerror ($command,$code,$stderr) * @return array */ function parallel_exec($commands, $onerror = null) { $spec = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')); $errors = array(); $ret = array(); foreach (array_chunk($commands, hw_core_get(), true) as $chunk) { $processes = array(); foreach ($chunk as $key => $command) { $key = to_hash($key); $process = proc_open($command, $spec, $pipes); if (is_resource($process)) { fclose($pipes[0]); $processes[$key] = array('cmd' => $command, 'res' => $process, 'out' => $pipes[1], 'err' => $pipes[2]); } else { $processes[$key] = $command; } } while (!empty($processes)) { foreach ($processes as $key => $v) { $resource = $v['res']; $cmd = $v['cmd']; if (is_resource($resource)) { $status = proc_get_status($resource); if (!$status['running']) { $code = $status['exitcode']; $out = stream_get_contents($v['out']); fclose($v['out']); $err = stream_get_contents($v['err']); fclose($v['err']); proc_close($resource); unset($processes[$key]); if (0 !== $code) { $errors[$key] = array('error' => "Error code '{$code}'", 'cmd' => $cmd, 'err' => $err); } else { $ret[$key] = $out; } } } else { unset($processes[$key]); $errors[$key] = array('error' => "Could not open process", 'cmd' => $cmd); } } } } if (null !== $onerror) { foreach ($errors as $error) { $onerror($error['cmd'], $error['error'], array_key_exists('err', $error) ? $error['err'] : null); } } else { $format_errors = function ($errors) { $errors = array_map_val($errors, function ($error) { global $cli_format_error; return $cli_format_error($error['cmd'], $error['error'], array_key_exists('err', $error) ? $error['err'] : null); }); return implode(PHP_EOL . '===' . PHP_EOL, $errors); }; debug_enforce(empty($errors), $format_errors($errors)); } return $ret; }
/** * Convert HTML string to image file * @param string $html * @param string $target path * @param array $options */ function wk_conv_str_to_img_file($html, $target, $options) { $source = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('ifr_wkhtmltox') . '.html'; debug_enforce(false !== file_put_contents($source, $html), "Cannot create temporary file {$source}"); wk_conv_file_to_img_file($source, $target, $options); debug_enforce(unlink($source), "Cannot delete temporary file '{$source}'"); }
/** * @return int */ function htmlspecialchars_flags() { switch (Doctype::get()) { case Doctype::VER_HTML_4_01: $flags = ENT_HTML401; break; case Doctype::VER_HTML_5: $flags = ENT_HTML5; break; default: if (Doctype::isXhtml()) { $flags = ENT_XHTML; } else { debug_enforce(false, "Unhandled doctype " . Doctype::get()); $flags = null; } break; } return $flags; }
/** * @param callable $callable * @param mixed $userData * @return callable function($array) */ function array_walk_dg($callable, $userData = null) { return function ($array) use($callable, $userData) { debug_enforce(array_walk($array, $callable, $userData), 'array_walk failure'); return $array; }; }
/** * @param string $str * @return int */ function str_to_uint($str) { debug_enforce_string($str); debug_enforce(0 !== strlen($str), "Cannot convert empty string to uint"); $ret = (int) $str; debug_enforce_gte($ret, 0); return $ret; }
/** * @param Zend_Db_Select|null $q * @param bool $collection * @return Lib_Model_Db * * @deprecated * @see loadOne */ public function getOne($q = null, $collection = false) { $ret = $this->load($q, $collection); $count = count($ret); debug_enforce(1 === $count, "getOne expects single result, but {$count} given"); return array_shift($ret); }
/** * @param array $config * * @return MongoDB */ public function getMongoDb($config) { if (class_exists('MongoClient')) { $class = 'MongoClient'; } else { $class = 'Mongo'; debug_enforce(class_exists($class), "No 'Mongo' class available. Make sure you've installed php mongo extension correctly."); } $confOptions = array(); if (!empty($config["mongo_username"])) { $confOptions["username"] = $config["mongo_username"]; } if (!empty($config["mongo_password"])) { $confOptions["password"] = $config["mongo_password"]; } /** @var $_conn Mongo */ $_conn = new $class('mongodb://' . $config['mongo_host'], $confOptions); $dbMongo = $_conn->selectDB($config["db_name"]); $dbMongo->listCollections(); return $dbMongo; }
/** * @param string $uri_data * @return array * @throws Exception */ function uri_data_unpack($uri_data) { debug_enforce(preg_match('/^data:([^;,]+)?;?([^;,]+)?;?(base64)?,(.+)$/', $uri_data, $matches), 'Malformed data uri: ' . var_dump_human_compact($uri_data)); return array_rest($matches, 1); }
function file_copy($source, $destination) { debug_enforce(copy($source, $destination), "Cannot copy file from " . var_dump_human_compact($source) . " to " . var_dump_human_compact($destination)); }