Skip to content

Commit ff28a7a

Browse files
committed
Issue 23 dotkernel.com
1 parent 0123948 commit ff28a7a

14 files changed

Lines changed: 590 additions & 164 deletions

File tree

bin/generate-feed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
use Light\App\Service\FeedGenerator;
7+
8+
chdir(__DIR__ . '/../');
9+
10+
require 'vendor/autoload.php';
11+
12+
$container = require 'config/container.php';
13+
$feedGenerator = $container->get(FeedGenerator::class);
14+
15+
$count = $feedGenerator->write();
16+
17+
printf(
18+
"Done. %d article%s written to %s%s",
19+
$count,
20+
$count === 1 ? '' : 's',
21+
$feedGenerator->getFeedFile(),
22+
PHP_EOL
23+
);

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"require": {
2929
"php": "~8.5.0",
30+
"ext-dom": "*",
3031
"doctrine/data-fixtures": "^2.2",
3132
"doctrine/doctrine-fixtures-bundle": "^4.3",
3233
"dotkernel/dot-errorhandler": "^5.0.0",
@@ -69,7 +70,8 @@
6970
"@development-enable"
7071
],
7172
"post-update-cmd": [
72-
"php bin/composer-post-install-script.php"
73+
"php bin/composer-post-install-script.php",
74+
"php bin/generate-feed.php"
7375
],
7476
"development-disable": "laminas-development-mode disable",
7577
"development-enable": "laminas-development-mode enable",

config/autoload/app.global.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
return [
2424
'app' => $app,
25+
'feed' => [
26+
'file' => 'public/feed.xml',
27+
],
2528
'twig' => [
2629
'globals' => [
2730
'app' => $app,

public/feed.xml

Whitespace-only changes.

src/App/src/ConfigProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
use Dot\Cache\Adapter\FilesystemAdapter;
1313
use Light\App\DBAL\Types\UuidType;
1414
use Light\App\Factory\EntityListenerResolverFactory;
15+
use Light\App\Factory\FeedGeneratorFactory;
16+
use Light\App\Factory\GetFeedViewHandlerFactory;
1517
use Light\App\Factory\GetIndexViewHandlerFactory;
18+
use Light\App\Handler\GetFeedViewHandler;
1619
use Light\App\Handler\GetIndexViewHandler;
1720
use Light\App\Resolver\EntityListenerResolver;
21+
use Light\App\Service\FeedGenerator;
1822
use Mezzio\Application;
1923
use Roave\PsrContainerDoctrine\EntityManagerFactory;
2024
use Symfony\Component\Cache\Adapter\AdapterInterface;
@@ -109,6 +113,8 @@ public function getDependencies(): array
109113
'doctrine.entity_manager.orm_default' => EntityManagerFactory::class,
110114
EntityListenerResolver::class => EntityListenerResolverFactory::class,
111115
GetIndexViewHandler::class => GetIndexViewHandlerFactory::class,
116+
GetFeedViewHandler::class => GetFeedViewHandlerFactory::class,
117+
FeedGenerator::class => FeedGeneratorFactory::class,
112118
],
113119
'aliases' => [
114120
EntityManager::class => 'doctrine.entity_manager.orm_default',
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\App\Factory;
6+
7+
use Light\App\Service\FeedGenerator;
8+
use Light\Blog\Repository\PostRepository;
9+
use Psr\Container\ContainerInterface;
10+
11+
use function assert;
12+
use function rtrim;
13+
14+
class FeedGeneratorFactory
15+
{
16+
public function __invoke(ContainerInterface $container): FeedGenerator
17+
{
18+
$postRepository = $container->get(PostRepository::class);
19+
assert($postRepository instanceof PostRepository);
20+
21+
$config = $container->get('config');
22+
23+
return new FeedGenerator(
24+
$postRepository,
25+
$config['feed']['file'],
26+
rtrim($config['application']['url'] ?? '', '/') . '/',
27+
$config['app']['meta']['title'] ?? '',
28+
$config['app']['meta']['description'] ?? '',
29+
);
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\App\Factory;
6+
7+
use Light\App\Handler\GetFeedViewHandler;
8+
use Light\App\Service\FeedGenerator;
9+
use Psr\Container\ContainerInterface;
10+
11+
use function assert;
12+
13+
class GetFeedViewHandlerFactory
14+
{
15+
/**
16+
* @param class-string $requestedName
17+
*/
18+
public function __invoke(ContainerInterface $container, string $requestedName): GetFeedViewHandler
19+
{
20+
$feedGenerator = $container->get(FeedGenerator::class);
21+
assert($feedGenerator instanceof FeedGenerator);
22+
23+
return new GetFeedViewHandler($feedGenerator);
24+
}
25+
}

src/App/src/Fixture/PostLoader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function load(ObjectManager $manager): void
7575
: new DateTimeImmutable();
7676

7777
$excerpt = $articleData['excerpt'] ?? '';
78+
$tlDr = $articleData['tl_dr'] ?? '';
7879

7980
$article = $repository->findOneBy(['slug' => $slug]);
8081

@@ -87,6 +88,7 @@ public function load(ObjectManager $manager): void
8788
$article->setCategory($category);
8889
$article->setAuthor($author);
8990
$article->setExcerpt($excerpt);
91+
$article->setTldr($tlDr);
9092

9193
$manager->persist($article);
9294
echo "CREATE: {$title}\n";
@@ -117,6 +119,10 @@ public function load(ObjectManager $manager): void
117119
$article->setExcerpt($excerpt);
118120
$changed = true;
119121
}
122+
if ($article->getTldr() !== $tlDr) {
123+
$article->setTldr($tlDr);
124+
$changed = true;
125+
}
120126

121127
echo $changed ? "UPDATE: {$title}\n" : "UNCHANGED: {$title}\n";
122128
}

0 commit comments

Comments
 (0)