/
bear
/
my_snippets
Обзор
Документация
Войти
/
bear
/
my_snippets
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
data/php_snippets.xml
130 строк
11 KB
Nikita Kalitin
add(data): php_snippets
22 ноя 2019, 17:07
22 ноя 2019, 17:07
d1675ab
Код
Авторство
О чём код?
<templateSet group="php_snippets"> <template name="pdo_connect" value="//---------------------------------------------------------------// $host = 'localhost'; $db = '$NAMEDB$'; $user = '$NAMEUSER$'; $pass = '$PASSKEY$'; $charset = 'utf8mb4'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = array( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::ATTR_EMULATE_PREPARES => false, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET utf8" ); //$pdo = new \PDO($dsn, $user, $pass, $opt); try { $pdo = new \PDO($dsn, $user, $pass, $opt); } catch (\PDOException $e) { throw new \PDOException($e->getMessage(), (int)$e->getCode()); } //---------------------------------------------------------------//" description="настройка подключения к БД" toReformat="false" toShortenFQNames="true"> <variable name="NAMEDB" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="NAMEUSER" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="PASSKEY" expression="" defaultValue="" alwaysStopAt="true" /> <context> <option name="PHP" value="true" /> </context> </template> <template name="pdo_request" value="/********************************************************/ $Param=$PARAM$; $sqlquery = "SELECT * FROM $TABLE$ WHERE $COLUMN$ = ? LIMIT 1;"; $stmt =$pdo->prepare($sqlquery); $stmt->execute(array($PARAM$)); $data = $stmt->fetchAll(); foreach($data as $row) { print_r($row); } /********************************************************/" description="запрос к БД" toReformat="false" toShortenFQNames="true"> <variable name="PARAM" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="TABLE" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="COLUMN" expression="" defaultValue="" alwaysStopAt="true" /> <context> <option name="PHP" value="true" /> </context> </template> <template name="pdo_transact" value="/** PDO TRANSACTION */ /*fixme: check this, or add tpl */ $querySelect="SELECT * FROM table1 where id_user= :id_user"; $queryInsert="INSERT INTO table2 (val1, val2) VALUES(:num1,:color1), (:num2,:color2)"; $pdo->beginTransaction();//начало try { $stmt = $pdo->prepare($querySelect); $stmt->execute(array(":id_user"=>25)); $res1=$stmt->fetchAll(); $stmt = $pdo->prepare($queryInsert); $stmt->execute(array(":num1"=>1,":color1"=>'red',":num2"=>2,":color2"=>'green')); $rew2=$stmt->fetchAll(); } catch (Exception $e) { $pdo->rollBack();//откат header('Content-Type: application/json', true, 500); echo json_encode(["result:" => "db_error!", "request" => $e->getMessage()]); } $pdo->commit();//фиксация /** End of pdo transacction */" description="шаблон транзакции (есть нюансы, см. офиц. документацию)" toReformat="false" toShortenFQNames="true"> <context> <option name="PHP" value="true" /> </context> </template> <template name="curl_debug" value="/***************************************************/ $fp_err = fopen($_SERVER['DOCUMENT_ROOT'].'/verbose_file.txt', 'ab+'); fwrite($fp_err, date('Y-m-d H:i:s')."\n\n"); //add timestamp to the verbose log	 curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_FAILONERROR, true); curl_setopt($ch, CURLOPT_STDERR, $fp_err); /****************************************************/" description="использование отладки curl" toReformat="false" toShortenFQNames="true"> <context> <option name="PHP" value="true" /> </context> </template> <template name="setLog_0x" value="//*************************************************************************************// define('LOG_PATH_0x', __DIR__ . '/loggers_0x_2.log'); function setLog_0x($str, $file = LOG_PATH_0x) { if ($file === "default") { $file = LOG_PATH; } $tmp_file = fopen($file, 'a'); $date = date('Y-m-d H:i:s'); $str_res = $date . " => " . $str; fwrite($tmp_file, $str_res . "\r\n"); fclose($tmp_file); return 0; } //*************************************************************************************//" description="logger simple" toReformat="false" toShortenFQNames="true"> <context> <option name="PHP" value="true" /> </context> </template> <template name="get_json" value="// Takes raw data from the request $json = file_get_contents('php://input'); // Converts it into a PHP object $data = json_decode($json);" description="template for get json from raw_data (data was been sended with header 'Application/json')" toReformat="false" toShortenFQNames="true" /> <template name="eco" value="echo "$END$";" description="'echo' statement" toReformat="true" toShortenFQNames="true"> <context> <option name="PHP Statement" value="true" /> </context> </template> <template name="fore" value="foreach ($ITERABLE$ as $VAR_VALUE$) { $END$ }" description="foreach(iterable_expr as $value) {...}" toReformat="true" toShortenFQNames="true"> <variable name="ITERABLE" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="VAR_VALUE" expression="phpSuggestVariableName()" defaultValue=""$item"" alwaysStopAt="true" /> <context> <option name="PHP Statement" value="true" /> </context> </template> <template name="forek" value="foreach ($ITERABLE$ as $VAR_KEY$ => $VAR_VALUE$) { $END$ }" description="foreach(iterable_expr as $key => $value) {...}" toReformat="true" toShortenFQNames="true"> <variable name="ITERABLE" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="VAR_KEY" expression="phpSuggestVariableName()" defaultValue=""$index"" alwaysStopAt="true" /> <variable name="VAR_VALUE" expression="phpSuggestVariableName()" defaultValue=""$item"" alwaysStopAt="true" /> <context> <option name="PHP Statement" value="true" /> </context> </template> <template name="inc" value="include "$END$";" description="'include' statement" toReformat="true" toShortenFQNames="true"> <context> <option name="PHP Expression" value="true" /> <option name="PHP Statement" value="true" /> </context> </template> <template name="inco" value="include_once "$END$";" description="'include_once' statement" toReformat="true" toShortenFQNames="true"> <context> <option name="PHP Expression" value="true" /> <option name="PHP Statement" value="true" /> </context> </template> <template name="prif" value="private function $NAME$($PARAMETERS$){ $END$ }" description="private function" toReformat="true" toShortenFQNames="true"> <variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="PARAMETERS" expression="phpVar" defaultValue="" alwaysStopAt="true" /> <context> <option name="PHP Class Member" value="true" /> </context> </template> <template name="prisf" value="private static function $NAME$($PARAMETERS$){ $END$ }" description="private static function" toReformat="true" toShortenFQNames="true"> <variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="PARAMETERS" expression="phpVar" defaultValue="" alwaysStopAt="true" /> <context> <option name="PHP Class Member" value="true" /> </context> </template> <template name="prof" value="protected function $NAME$($PARAMETERS$){ $END$ }" description="protected function" toReformat="true" toShortenFQNames="true"> <variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="PARAMETERS" expression="phpVar" defaultValue="" alwaysStopAt="true" /> <context> <option name="PHP Class Member" value="true" /> </context> </template> <template name="prosf" value="protected static function $NAME$($PARAMETERS$){ $END$ }" description="protected static function" toReformat="true" toShortenFQNames="true"> <variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="PARAMETERS" expression="phpVar" defaultValue="" alwaysStopAt="true" /> <context> <option name="PHP Class Member" value="true" /> </context> </template> <template name="pubf" value="public function $NAME$($PARAMETERS$){ $END$ }" description="public function" toReformat="true" toShortenFQNames="true"> <variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="PARAMETERS" expression="phpVar" defaultValue="" alwaysStopAt="true" /> <context> <option name="PHP Class Member" value="true" /> </context> </template> <template name="pubsf" value="public static function $NAME$($PARAMETERS$){ $END$ }" description="public static function" toReformat="true" toShortenFQNames="true"> <variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="PARAMETERS" expression="phpVar" defaultValue="" alwaysStopAt="true" /> <context> <option name="PHP Class Member" value="true" /> </context> </template> <template name="rqr" value="require "$END$";" description="'require' statement" toReformat="true" toShortenFQNames="true"> <context> <option name="PHP Expression" value="true" /> <option name="PHP Statement" value="true" /> </context> </template> <template name="rqro" value="require_once "$END$";" description="'require_once' statement" toReformat="true" toShortenFQNames="true"> <context> <option name="PHP Expression" value="true" /> <option name="PHP Statement" value="true" /> </context> </template> <template name="thr" value="throw new $END$" description="throw new" toReformat="true" toShortenFQNames="true"> <context> <option name="PHP Statement" value="true" /> </context> </template> <template name="recursive_iterator" value="//------------------------------------------------------------------------------- $raw_array = [[1],["2"=>[3,4,5,6,7],"8"=>[9,10,11]],["a","b","c"],[[[d]]]]; $flatten_array = []; $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($raw_array)); foreach($iterator as $value) { array_push($flatten_array, $value); } print_r($flatten_array); //-------------------------------------------------------------------------------" description="SPL рекурсивный итератор" toReformat="false" toShortenFQNames="true"> <context> <option name="PHP" value="true" /> </context> </template> </templateSet>