Skip to content

Instantly share code, notes, and snippets.

View Nex-Otaku's full-sized avatar

Леонид Черненко Nex-Otaku

View GitHub Profile
@Nex-Otaku
Nex-Otaku / InfluxdbLine.php
Last active April 2, 2021 15:43
Serializing metrics to InfluxDB line format
<?php
class InfluxdbLine
{
// https://docs.influxdata.com/influxdb/v2.0/reference/syntax/line-protocol/
public function format(string $measurement, array $tags, array $fields, string $timestampNanoSeconds): string
{
$measurementEscaped = $this->escapeMeasurement($measurement);
$tagsFlat = $this->flattenTags($tags);
$fieldsFlat = $this->flattenFields($fields);
@Nex-Otaku
Nex-Otaku / CachedSchemaTrait.php
Last active March 26, 2021 09:13
Yii2 trait for caching table schema in memory, speeds up creating object significantly
<?php
/**
* Use this in ActiveRecord models to speed up creating objects 10x times
*/
trait CachedSchemaTrait
{
public static function getTableSchema()
{
static $cache;
@Nex-Otaku
Nex-Otaku / yii3-stats.js
Created March 23, 2021 10:51
Get Yii 3 release progress
const got = require('got');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const url = 'https://www.yiiframework.com/status/3.0';
const isStableVersion = (version) => {
if (version === '') {
return false;
}
@Nex-Otaku
Nex-Otaku / routes.php
Created March 20, 2021 16:22
Yii3 routes example
<?php
declare(strict_types=1);
use App\Auth\AuthController;
use App\User\UserController;
use App\Middleware\ApiDataWrapper;
use App\Middleware\CorsHeaders;
use App\Middleware\RequestFormParser;
use App\RequestForm\SignupForm;
@Nex-Otaku
Nex-Otaku / kill-yii-server.sh
Last active March 16, 2021 12:42
Bash script to stop Yii server
#!/bin/bash
YII_SERVER_PGID=$(ps x -o pgid,command | grep -v grep | grep "/vendor/bin/yii serve" | awk '{print $1;}')
if [[ -z "$YII_SERVER_PGID" ]]; then
echo "Yii server not running."
else
kill -TERM -- -$YII_SERVER_PGID
echo "Killed Yii server."
fi
@Nex-Otaku
Nex-Otaku / ShellCommandDraft.php
Last active March 1, 2021 20:57
REPL shell command draft for Yii 3 application
<?php
namespace App\Command\Shell;
use Psy\Configuration;
use Psy\Shell;
use Psy\VersionUpdater\Checker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@Nex-Otaku
Nex-Otaku / BreakSignalDetector.php
Created February 26, 2021 13:47
Detecting system signals to gracefully stop PHP script
/**
* Usage:
*
* $breakSignalDetector = new BreakSignalDetector();
* $breakSignalDetector->registerSignalHandler();
*
* while (...) // Some long running cycle
* {
* ...
* if ($breakSignalDetector->isTerminated()) {
@Nex-Otaku
Nex-Otaku / covariance.php
Created February 18, 2021 08:33
Covariance in PHP
<?php
// Covariance - narrowing type on return. Works from PHP 7.4.0 and above
// https://www.php.net/manual/ru/language.oop5.variance.php
class Animal
{
public function getName(): string
{
return 'Some animal';
@Nex-Otaku
Nex-Otaku / DependencyInjectionDraft.php
Created February 16, 2021 14:46
Dependency Injection supported on language level in PHP
<?php
// Now:
class Shop
{
private Log $log;
public function __construct(Log $log)
{
@Nex-Otaku
Nex-Otaku / json-paths.php
Created February 12, 2021 09:58
2021-02-12 PHP_VRN Challenge
<?php
// Есть вот такой json: https://api.jsonbin.io/b/600e00febca934583e41307f
// На самом деле, на него надо смотреть как на какой-то json, в котором может быть есть массивы и/или объекты. Какие-то. Есть ли они вообще и сколько их достоверно не известно. Известно только то, что на вход подается валидный json.
// Задача: вывести все пути к массивам. Конкатенация пути через точку. Например в примере выше ключ bookkeeping указывает не на массив, а на другой объект. Путь bookkeeping.contacts указывает тоже не на массив, а на объект, а вот bookkeeping.contacts.emails указывает на массив и это первый найденный путь. Задача найти их всех.
// Правильный ответ для этого примера по ссылке
//