Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PHP 8 and cleanups / modernization. #15

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.2.0",
"license": "Apache-2.0",
"require": {
"php": "^7.2.5",
"php": "^8.0",
"symfony/filesystem": "^5.2.4",
"twig/twig": "^3.3.0",
"symfony/http-foundation": "^5.2.4",
Expand All @@ -21,7 +21,9 @@
"symfony/dom-crawler": "^5.2.4",
"justinrainbow/json-schema": "^5.2",
"spatie/image-optimizer": "^1.3.2",
"ext-imagick": "*"
"ext-imagick": "*",
"ext-fileinfo": "*",
"ext-exif": "*"
},
"autoload": {
"psr-4": {
Expand Down
828 changes: 488 additions & 340 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/API/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public function create(Request $request, $path)

public function read(Request $request, $path): JsonResponse
{
if (strpos($path, '..') !== false) {
if (str_contains($path, '..')) {
return new JsonResponse([
'error' => 'Invalid location provided.'
], 400);
};
}

$finder = new Finder();

Expand Down Expand Up @@ -132,7 +132,7 @@ public function update(Request $request, $path): JsonResponse

public function delete(Request $request, $path): JsonResponse
{
if (strpos($path, '..') !== false) {
if (str_contains($path, '..')) {
return new JsonResponse([
'error' => 'Invalid file path'
], 400);
Expand Down
13 changes: 7 additions & 6 deletions src/API/ModelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function read(Request $request, $id)
if ($id) return new Response(null, 404);
try {
$record = Record::fromDisk($this->model);
} catch (RecordNotFoundException $e) {
} catch (RecordNotFoundException) {
$record = new Record($this->model);
}
return new JsonResponse($record);
Expand All @@ -76,7 +76,7 @@ public function read(Request $request, $id)
$sort = $request->get('sort');
if ($sort) {
$sortDirection = 'asc';
if (substr($sort, 0, 1) === '-') {
if (str_starts_with($sort, '-')) {
$sortDirection = 'desc';
$sort = substr($sort, 1);
}
Expand All @@ -90,7 +90,7 @@ private function readOne(ContentLoader $loader, $id)
try {
$record = $loader->find($id);
return new JsonResponse($record);
} catch (Exception $e) {
} catch (Exception) {
}

return new Response(null, 404);
Expand All @@ -101,7 +101,7 @@ private function readMany(ContentLoader $loader): JsonResponse
try {
$records = $loader->withChildren();
return new JsonResponse(array_values(iterator_to_array($records)));
} catch (Exception $e) {
} catch (Exception) {
}

return new JsonResponse([]);
Expand Down Expand Up @@ -181,7 +181,7 @@ public function delete(Request $request, $id): JsonResponse
return new JsonResponse([
'message' => 'Delete successful.'
]);
} catch (RecordNotFoundException $e) {
} catch (RecordNotFoundException) {
return new JsonResponse([
'error' => 'Not found.'
], 403);
Expand All @@ -195,9 +195,10 @@ public function delete(Request $request, $id): JsonResponse
*/
public function describe(Request $request): JsonResponse
{
$blocks = $this->model->getBlocks();
return new JsonResponse([
'model' => $this->model,
'blocks' => $this->model->getBlocks()
'blocks' => count($blocks) > 0 ? $blocks : new \ArrayObject()
]);
}

Expand Down
6 changes: 3 additions & 3 deletions src/API/OpenApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private function getInfo(): array
$title = $siteRecord->getValue('title');
return [
'title' => $title,
'description' => "API documentation for {$title}"
'description' => "API documentation for $title"
];
}

Expand All @@ -52,9 +52,9 @@ private function getPaths()
$modelType = $model->getType();
$modelName = $model->getName();
$verb = $model->isSingle() ? 'Get' : 'List';
$paths["/api/{$modelType}"] = [
$paths["/api/$modelType"] = [
'get' => [
'summary' => "{$verb} {$modelName}",
'summary' => "$verb $modelName",
'responses' => [
'200' => [
'description' => 'Successful operation',
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected function configure()
{
$this
->setName('build')
->addOption('host', null, InputOption::VALUE_REQUIRED, '')
->addOption('host', null, InputOption::VALUE_REQUIRED)
->addOption('scheme', 's', InputOption::VALUE_OPTIONAL, '', 'http')
->setDescription('Builds static HTML files for your website.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
putenv('BABBLE_LIVE_RELOAD=true');
}

$command = 'php -S localhost:8000 index.php';
$command = 'php -S localhost:8000';
$handle = popen($command, 'r');
while (1) {
$read = fread($handle, 1024);
Expand Down
3 changes: 1 addition & 2 deletions src/Content/ContentLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ private function initIterator()
if ($this->filters->isMatch($record)) {
// Matching record was found.
$records[] = new TemplateRecord($record);
continue;
}
}

Expand All @@ -178,7 +177,7 @@ private function initIterator()
}

$this->arrayIterator = new ArrayIterator($records);
} catch (InvalidArgumentException $e) {
} catch (InvalidArgumentException) {
$this->arrayIterator = new ArrayIterator([]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Content/Filters/WhereFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function isMatch(Record $record): bool
case '<=':
return $field->isLessOrEqual($key, $this->value);
case '>=':
return $field->isGreater($key, $this->value);
return $field->isGreaterOrEqual($key, $this->value);
}

throw new Exception('Invalid comparison operator "' . $this->comparison . '".');
throw new \Exception('Invalid comparison operator "' . $this->comparison . '".');
}
}
4 changes: 2 additions & 2 deletions src/Content/StaticSiteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private function render(Path $pathObject)

// Save and log success.
$this->save($pathObject, $html);
$this->log($pathObject->clean(), 'info');
$this->log($pathObject->clean());

// Look for links on the rendered page, and render them.
$crawler = new Crawler($html);
Expand All @@ -144,7 +144,7 @@ private function render(Path $pathObject)
}

// Skip absolute paths without protocol, static directory and uploads directory.
if (strpos($path, '/static/') === 0 || strpos($path, '/uploads/') === 0) {
if (str_starts_with($path, '/static/') || str_starts_with($path, '/uploads/')) {
continue;
}

Expand Down
10 changes: 6 additions & 4 deletions src/Models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class BaseModel implements JsonSerializable
{
Expand All @@ -25,7 +27,7 @@ public function __construct(string $type)
{
try {
$this->init($type);
} catch (ParseException $e) {
} catch (ParseException) {
throw new InvalidModelException('Invalid definition: ' . $type);
}
}
Expand Down Expand Up @@ -88,7 +90,7 @@ protected function initName(array $modelFormat)
$this->name = $modelFormat['name'];
if (!empty($modelFormat['name_plural'])) {
$this->namePlural = $modelFormat['name_plural'];
} else if (substr($this->name, strlen($this->name) - 1) === 's') {
} else if (str_ends_with($this->name, 's')) {
$this->namePlural = $this->name;
} else {
$this->namePlural = $this->name . 's';
Expand Down Expand Up @@ -168,8 +170,8 @@ public function getCacheLocation(string $recordId)
protected function initProperties(array $properties)
{
// TODO: Validation.
$loader = new \Twig\Loader\ArrayLoader($properties);
$twig = new \Twig\Environment($loader);
$loader = new ArrayLoader($properties);
$twig = new Environment($loader);
$this->properties = $twig;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function toJSON($value)

protected function initOptions(array $data)
{
$this->options = $data['options'] ?? [];
$this->options = $data['options'] ?? new \ArrayObject();
}

private function initValidation($data)
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Fields/ImageField.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function crop($width = null, $height = null)
$sourceFilename = absPath('public' . $path);
try {
$img = $manager->make($sourceFilename);
} catch (NotReadableException $e) {
} catch (NotReadableException) {
return '';
}

Expand Down
2 changes: 0 additions & 2 deletions src/Models/Fields/ListField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@


use ArrayAccess;
use Babble\Models\BaseModel;
use Babble\Models\Block;
use Babble\Models\Model;
use Babble\Models\Record;

class ListField extends Field
Expand Down
3 changes: 2 additions & 1 deletion src/Models/Fields/TagsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Babble\Models\Fields;

use ArrayIterator;
use Babble\Models\Record;
use Cocur\Slugify\Slugify;
use Traversable;
Expand Down Expand Up @@ -66,7 +67,7 @@ public function contains(string $tag): bool
*/
public function getIterator()
{
return new \ArrayIterator($this->tags);
return new ArrayIterator($this->tags);
}


Expand Down