Last active
May 12, 2022 12:59
-
-
Save vudaltsov/7036b01a7bee89ad0c4b4b2485e91409 to your computer and use it in GitHub Desktop.
PHP facade functions examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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