Skip to content

Commit

Permalink
fix: compatibility with new version of slim
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroTroller committed Feb 6, 2023
1 parent a875b66 commit f4c9dbb
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 65 deletions.
7 changes: 5 additions & 2 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use PedroTroller\CS\Fixer\Fixers;
use PedroTroller\CS\Fixer\RuleSetFactory;

return PhpCsFixer\Config::create()
$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules(RuleSetFactory::create()
->symfony(true)
Expand All @@ -27,7 +28,9 @@
->registerCustomFixers(new Fixers())
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__ . '/html')
->in(__DIR__.'/html')
->append([__FILE__])
)
;

return $config;
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "pedrotroller/http-markup",
"require": {
"react/promise": "^2.8",
"slim/psr7": "^1.6",
"slim/slim": "^4.11",
"symfony/process": "^5.1"
},
Expand Down
228 changes: 183 additions & 45 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 14 additions & 18 deletions html/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Symfony\Component\Process\Process;
use Slim\App;
use React\Promise\Deferred;
use React\Promise\Promise;
use Slim\Factory\AppFactory;
use Symfony\Component\Process\Process;

require_once dirname(__DIR__).'/vendor/autoload.php';

$app = new App();
$app = AppFactory::create();

$app->post('/', function (Request $request, Response $response, array $args): Response {
$result = [
'status' => 502,
'body' => '',
'body' => '',
];

$deferred = new Deferred();
$promise = $deferred->promise();
$promise = $deferred->promise();

$promise
->then(function (Request $request): array {
$type = $request->getHeader('Content-Type');

if (empty($type)) {
throw new Exception('No Content-Type provided', 415);
throw new Exception('No Content-Type provided.', 415);
}

if (is_array($type)) {
Expand All @@ -49,18 +48,15 @@
];

if (false === array_key_exists($type, $formats)) {
throw new Exception(
sprintf('Unsupported Content-Type. Only %s supported', implode(', ', array_keys($formats))),
415,
);
throw new Exception(sprintf('Unsupported Content-Type. Only %s supported.', implode(', ', array_keys($formats))), 415);
}

return [$formats[$type], (string) $request->getBody()];
})
->then(function (array $data): string {
[$extension, $markup] = $data;
$temporaryFile = sprintf('%s/%s.%s', sys_get_temp_dir(), uniqid(), $extension);
$process = new Process(
$temporaryFile = sprintf('%s/%s.%s', sys_get_temp_dir(), uniqid(), $extension);
$process = new Process(
[
'github-markup',
$temporaryFile,
Expand All @@ -79,7 +75,7 @@
})
->then(function (string $html): string {
$temporaryFile = sprintf('%s/%s.%s', sys_get_temp_dir(), uniqid(), 'html');
$process = new Process(
$process = new Process(
[
'prettier',
'--write',
Expand All @@ -105,13 +101,13 @@

throw new Exception($process->getErrorOutput());
})
->otherwise(function (Exception $exception) use (&$result) {
->otherwise(function (Exception $exception) use (&$result): void {
$result['status'] = $exception->getCode() ?: 500;
$result['body'] = $exception->getMessage();
$result['body'] = $exception->getMessage();
})
->then(function (string $html) use (&$result): void {
$result['status'] = 200;
$result['body'] = $html;
$result['body'] = $html;
})
;

Expand All @@ -123,7 +119,7 @@
});

$app->get('/_ping', function (Request $request, Response $response, array $args): Response {
return $response->withStatus(200);
return $response->withStatus(200);
});

$app->run();

0 comments on commit f4c9dbb

Please sign in to comment.