Skip to content

Commit 41a5c0f

Browse files
committed
Remove public Slugger services and container fixtures dependency
1 parent f4a58f5 commit 41a5c0f

File tree

6 files changed

+15
-23
lines changed

6 files changed

+15
-23
lines changed

config/services.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,3 @@ services:
5151

5252
# needed for the 'localizeddate' Twig filter
5353
Twig\Extensions\IntlExtension: ~
54-
55-
# the slugger service needs a public alias for getting it from
56-
# the container when loading doctrine fixtures
57-
slugger:
58-
alias: App\Utils\Slugger
59-
public: true

src/Controller/Admin/BlogController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function indexAction()
7171
* to constraint the HTTP methods each controller responds to (by default
7272
* it responds to all methods).
7373
*/
74-
public function newAction(Request $request, Slugger $slugger)
74+
public function newAction(Request $request)
7575
{
7676
$post = new Post();
7777
$post->setAuthor($this->getUser());
@@ -87,7 +87,7 @@ public function newAction(Request $request, Slugger $slugger)
8787
// However, we explicitly add it to improve code readability.
8888
// See https://symfony.com/doc/current/best_practices/forms.html#handling-form-submits
8989
if ($form->isSubmitted() && $form->isValid()) {
90-
$post->setSlug($slugger->slugify($post->getTitle()));
90+
$post->setSlug(Slugger::slugify($post->getTitle()));
9191

9292
$em = $this->getDoctrine()->getManager();
9393
$em->persist($post);
@@ -135,15 +135,15 @@ public function showAction(Post $post)
135135
* @Route("/{id}/edit", requirements={"id": "\d+"}, name="admin_post_edit")
136136
* @Method({"GET", "POST"})
137137
*/
138-
public function editAction(Request $request, Post $post, Slugger $slugger)
138+
public function editAction(Request $request, Post $post)
139139
{
140140
$this->denyAccessUnlessGranted('edit', $post, 'Posts can only be edited by their authors.');
141141

142142
$form = $this->createForm(PostType::class, $post);
143143
$form->handleRequest($request);
144144

145145
if ($form->isSubmitted() && $form->isValid()) {
146-
$post->setSlug($slugger->slugify($post->getTitle()));
146+
$post->setSlug(Slugger::slugify($post->getTitle()));
147147
$this->getDoctrine()->getManager()->flush();
148148

149149
$this->addFlash('success', 'post.updated_successfully');

src/DataFixtures/ORM/PostFixtures.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
use App\DataFixtures\FixturesTrait;
1515
use App\Entity\Comment;
1616
use App\Entity\Post;
17+
use App\Utils\Slugger;
1718
use Doctrine\Common\DataFixtures\AbstractFixture;
1819
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
1920
use Doctrine\Common\Persistence\ObjectManager;
20-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
21-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
2221

2322
/**
2423
* Defines the sample blog posts to load in the database before running the unit
@@ -32,9 +31,8 @@
3231
* @author Javier Eguiluz <[email protected]>
3332
* @author Yonel Ceruto <[email protected]>
3433
*/
35-
class PostFixtures extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface
34+
class PostFixtures extends AbstractFixture implements DependentFixtureInterface
3635
{
37-
use ContainerAwareTrait;
3836
use FixturesTrait;
3937

4038
/**
@@ -47,7 +45,7 @@ public function load(ObjectManager $manager)
4745

4846
$post->setTitle($title);
4947
$post->setSummary($this->getRandomPostSummary());
50-
$post->setSlug($this->container->get('slugger')->slugify($post->getTitle()));
48+
$post->setSlug(Slugger::slugify($post->getTitle()));
5149
$post->setContent($this->getPostContent());
5250
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
5351

src/Utils/Slugger.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
namespace App\Utils;
1313

1414
/**
15-
* This class is used to provide an example of integrating simple classes as
16-
* services into a Symfony application.
17-
*
1815
* @author Ryan Weaver <[email protected]>
1916
* @author Javier Eguiluz <[email protected]>
2017
*/
@@ -25,7 +22,7 @@ class Slugger
2522
*
2623
* @return string
2724
*/
28-
public function slugify($string)
25+
public static function slugify($string)
2926
{
3027
return preg_replace('/\s+/', '-', mb_strtolower(trim(strip_tags($string)), 'UTF-8'));
3128
}

src/Utils/Validator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
namespace App\Utils;
1313

14+
/**
15+
* This class is used to provide an example of integrating simple classes as
16+
* services into a Symfony application.
17+
*
18+
* @author Javier Eguiluz <[email protected]>
19+
*/
1420
class Validator
1521
{
1622
public function validateUsername($username)

tests/Utils/SluggerTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ class SluggerTest extends \PHPUnit_Framework_TestCase
3030
*/
3131
public function testSlugify($string, $slug)
3232
{
33-
$slugger = new Slugger();
34-
$result = $slugger->slugify($string);
35-
36-
$this->assertSame($slug, $result);
33+
$this->assertSame($slug, Slugger::slugify($string));
3734
}
3835

3936
public function getSlugs()

0 commit comments

Comments
 (0)