Skip to content

Commit 39cc763

Browse files
committed
Add tags
1 parent e22cb8e commit 39cc763

21 files changed

Lines changed: 1580 additions & 228 deletions

public/css/app.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App/assets/scss/components/_custom.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,23 @@ section {
214214
margin: 0;
215215
}
216216

217+
.tag-badge {
218+
font-family: var(--mono);
219+
font-size: 13px;
220+
color: var(--teal);
221+
background: var(--teal-dim);
222+
border: 1px solid var(--teal);
223+
border-radius: 6px;
224+
padding: 5px 12px;
225+
display: inline-block;
226+
text-decoration: none;
227+
228+
&:hover {
229+
background: var(--teal);
230+
color: var(--on-accent);
231+
}
232+
}
233+
217234
/* ============================================================
218235
Header / Bootstrap navbar
219236
============================================================ */

src/App/src/Fixture/PostLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ public function load(ObjectManager $manager): void
3838

3939
$repository = $manager->getRepository(Post::class);
4040
$usedSlugs = [];
41+
$postIndex = 0;
4142

4243
foreach ($categories as $cat) {
4344
/** @var Category $category */
4445
$category = $this->getReference('category_' . $cat['slug'], Category::class);
4546

4647
foreach ($cat['articles'] as $articleData) {
48+
$postIndex++;
49+
4750
$authorName = $articleData['author']['display_name'] ?? null;
4851
$authorSlug = $authorName ? $this->slugify($authorName) : null;
4952
if (! $authorSlug || ! $this->hasReference('author_' . $authorSlug, Author::class)) {
@@ -138,6 +141,8 @@ public function load(ObjectManager $manager): void
138141

139142
echo $changed ? "UPDATE: {$title}\n" : "UNCHANGED: {$title}\n";
140143
}
144+
145+
$this->addReference('post_' . $postIndex, $article);
141146
}
142147
}
143148

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\App\Fixture;
6+
7+
use Doctrine\Bundle\FixturesBundle\Fixture;
8+
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
9+
use Doctrine\Persistence\ObjectManager;
10+
use Light\Blog\Entity\Post;
11+
use Light\Blog\Entity\PostTag;
12+
use Light\Blog\Entity\Tag;
13+
use RuntimeException;
14+
15+
use function file_get_contents;
16+
use function json_decode;
17+
18+
class PostTagLoader extends Fixture implements DependentFixtureInterface
19+
{
20+
public function load(ObjectManager $manager): void
21+
{
22+
$jsonFile = __DIR__ . '/articles_cleaned.json';
23+
$contents = file_get_contents($jsonFile);
24+
25+
if ($contents === false) {
26+
throw new RuntimeException("Unable to read file: {$jsonFile}");
27+
}
28+
29+
$categories = json_decode($contents, true);
30+
31+
$repository = $manager->getRepository(PostTag::class);
32+
$postIndex = 0;
33+
34+
foreach ($categories as $cat) {
35+
foreach ($cat['articles'] as $articleData) {
36+
$postIndex++;
37+
38+
if (! $this->hasReference('post_' . $postIndex, Post::class)) {
39+
continue;
40+
}
41+
42+
/** @var Post $post */
43+
$post = $this->getReference('post_' . $postIndex, Post::class);
44+
45+
foreach ($articleData['tags'] ?? [] as $tagData) {
46+
/** @var Tag $tag */
47+
$tag = $this->getReference('tag_' . $tagData['slug'], Tag::class);
48+
49+
$postTag = $repository->findOneBy(['post' => $post, 'tag' => $tag]);
50+
51+
if ($postTag === null) {
52+
$postTag = new PostTag();
53+
$postTag->setPost($post);
54+
$postTag->setTag($tag);
55+
$manager->persist($postTag);
56+
echo "CREATE: {$post->getTitle()} - {$tag->getName()}\n";
57+
} else {
58+
echo "UNCHANGED: {$post->getTitle()} - {$tag->getName()}\n";
59+
}
60+
}
61+
}
62+
}
63+
64+
$manager->flush();
65+
}
66+
67+
public function getDependencies(): array
68+
{
69+
return [
70+
TagLoader::class,
71+
PostLoader::class,
72+
];
73+
}
74+
75+
public function getOrder(): int
76+
{
77+
return 4;
78+
}
79+
}

src/App/src/Fixture/TagLoader.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\App\Fixture;
6+
7+
use Doctrine\Bundle\FixturesBundle\Fixture;
8+
use Doctrine\Persistence\ObjectManager;
9+
use Light\Blog\Entity\Tag;
10+
use RuntimeException;
11+
12+
use function file_get_contents;
13+
use function json_decode;
14+
15+
class TagLoader extends Fixture
16+
{
17+
public function load(ObjectManager $manager): void
18+
{
19+
$jsonFile = __DIR__ . '/articles_cleaned.json';
20+
$contents = file_get_contents($jsonFile);
21+
22+
if ($contents === false) {
23+
throw new RuntimeException("Unable to read file: {$jsonFile}");
24+
}
25+
26+
$categories = json_decode($contents, true);
27+
28+
$repository = $manager->getRepository(Tag::class);
29+
$seenSlugs = [];
30+
31+
foreach ($categories as $cat) {
32+
foreach ($cat['articles'] as $article) {
33+
foreach ($article['tags'] ?? [] as $tagData) {
34+
$slug = $tagData['slug'];
35+
$name = $tagData['name'];
36+
37+
if (isset($seenSlugs[$slug])) {
38+
continue;
39+
}
40+
$seenSlugs[$slug] = true;
41+
42+
$tag = $repository->findOneBy(['slug' => $slug]);
43+
44+
if ($tag === null) {
45+
$tag = new Tag();
46+
$tag->setSlug($slug);
47+
$tag->setName($name);
48+
$manager->persist($tag);
49+
echo "CREATE: {$name}\n";
50+
} else {
51+
$changed = false;
52+
if ($tag->getName() !== $name) {
53+
$tag->setName($name);
54+
$changed = true;
55+
}
56+
57+
echo $changed ? "UPDATE: {$name}\n" : "UNCHANGED: {$name}\n";
58+
}
59+
60+
$this->addReference('tag_' . $slug, $tag);
61+
}
62+
}
63+
}
64+
65+
$manager->flush();
66+
}
67+
68+
public function getOrder(): int
69+
{
70+
return 1;
71+
}
72+
}

0 commit comments

Comments
 (0)