Skip to content

Instantly share code, notes, and snippets.

@vudaltsov
Last active May 12, 2022 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vudaltsov/7036b01a7bee89ad0c4b4b2485e91409 to your computer and use it in GitHub Desktop.
Save vudaltsov/7036b01a7bee89ad0c4b4b2485e91409 to your computer and use it in GitHub Desktop.
PHP facade functions examples
<?php
declare(strict_types=1);
/**
* @throws JsonException
*/
function jsonEncode(mixed $data, int $flags = 0): string
{
return json_encode($data, $flags | JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
}
/**
* @template TValue
* @template TValues of iterable<TValue>
* @param TValues $values
* @return (TValues is non-empty-array ? non-empty-list<TValue> : list<TValue>)
*/
function toList(iterable $values): array
{
if (is_array($values)) {
return array_values($values);
}
return iterator_to_array($values, false);
}
function tempFile(?string $prefix = null): string
{
return tempnam(sys_get_temp_dir(), $prefix ?? uniqid());
}
<?php
declare(strict_types=1);
namespace HappyInc\Infrastructure\Http;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
function fileResponse(string $file, string $name): BinaryFileResponse
{
$fileResponse = new BinaryFileResponse($file);
$fileResponse->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $name);
return $fileResponse;
}
<?php
declare(strict_types=1);
namespace HappyInc\Infrastructure\DependencyInjection;
use Symfony\Component\DependencyInjection\Loader\Configurator\InlineServiceConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\inline_service;
/**
* @param ?class-string $class
*/
function inlineService(?string $class = null, array $args = []): InlineServiceConfigurator
{
return inline_service($class)->args($args)->autowire();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment