Skip to content

Instantly share code, notes, and snippets.

View vyspiansky's full-sized avatar

Ihor Vyspiansky vyspiansky

View GitHub Profile
@vyspiansky
vyspiansky / largest-tables-in-mysql.sql
Last active May 6, 2025 12:23
Top 10 largest tables in a MySQL database
-- Top 10 largest tables in a MySQL database
-- MySQL 5.7.29
SELECT
table_schema AS `Database`,
table_name AS `Table`,
ROUND((data_length + index_length) / 1024 / 1024, 2) AS `Size (MB)`,
ROUND(data_length / 1024 / 1024, 2) AS `Data Size (MB)`,
ROUND(index_length / 1024 / 1024, 2) AS `Index Size (MB)`,
table_rows as 'Rows'
@vyspiansky
vyspiansky / add-remove-query-param-using-response.md
Last active April 15, 2025 19:24
Add new query parameter to URL using Symfony Response
// Create a request object or parse the URL
$request = \Symfony\Component\HttpFoundation\Request::create($urlWithoutToken);

// or get the current request.
// $request = $event->getRequest();

Add new query parameter to URL

@vyspiansky
vyspiansky / concurrent_http_requests.md
Last active March 18, 2025 12:12
Concurrent HTTP requests in PHP 8.1+

cURL Multi

Native cURL multi-handle API for parallel requests.

Using separate cURL requests

<?php
// File: separate_curl_requests.php
@vyspiansky
vyspiansky / simple_generator.php
Last active March 18, 2025 10:52
Simple generator in PHP
<?php
declare(strict_types=1);
function simpleGenerator() {
echo "The generator begins\n";
for ($i = 0; $i < 3; ++$i) {
yield $i;
echo "Yielded $i\n";
@vyspiansky
vyspiansky / max_allowed_packet-size-in-mysql.md
Last active March 6, 2025 20:26
Set max_allowed_packet size in MySQL

Update MySQL's maximum packet size configuration

We're going to fix the following issue

The query length of 118146958 bytes is larger than max_allowed_packet size (33554432).

First of all, let's check the current size

@vyspiansky
vyspiansky / file_extension_by_contents.md
Last active February 21, 2025 13:40
PHP: retrieve a file extension by its contents

Using Symfony's MimeTypes

use Symfony\Component\Mime\MimeTypes;

function getFileExtension(string $fileContent): ?string
{
    $mimeType = (new \finfo(FILEINFO_MIME_TYPE))->buffer($fileContent);

 $mimeTypes = new MimeTypes();
@vyspiansky
vyspiansky / move_file_to_directory_in_drupal.php
Last active March 18, 2025 10:52
Move file to directory in Drupal 10
<?php
use Drupal\Core\File\FileSystemInterface;
$directory = '/path/to/processed';
\Drupal::service('file_system')
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
\Drupal::service('file_system')
@vyspiansky
vyspiansky / extend_user_registration_form_in_drupal_10.md
Last active March 19, 2025 07:36
Extend a standard user registration form using classes in Drupal 10

To extend the standard user registration form, use

namespace Drupal\your_module\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\user\RegisterForm;

class YourRegisterForm extends RegisterForm {
 public function buildForm(array $form, FormStateInterface $form_state) {
@vyspiansky
vyspiansky / password_validation_message_in_drupal_10.md
Last active March 19, 2025 07:36
Change client-side password validation message in Drupal 10

We are going to replace "Make it at least 8 characters" from the user core modules (core/modules/user/user.module) with "Make it at least 12 characters".

/**
 * Implements hook_element_info_alter().
 */
function <your_module>_element_info_alter(array &$types): void
{
    if (isset($types['password_confirm'])) {
 $types['password_confirm']['#process'][] = 'users_form_process_password_confirm';
@vyspiansky
vyspiansky / ngrok_notes.md
Last active March 19, 2025 07:37
ngrok: create a tunnel from the public internet to your local machine

ngrok notes

ngrok creates a tunnel from the public internet to your local machine.

Install

Sign up for an ngrok account https://dashboard.ngrok.com/

brew install ngrok/ngrok/ngrok