Skip to content

Commit

Permalink
Remove public Slugger services and container fixtures dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Sep 4, 2017
1 parent f4a58f5 commit 41a5c0f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 23 deletions.
6 changes: 0 additions & 6 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,3 @@ services:

# needed for the 'localizeddate' Twig filter
Twig\Extensions\IntlExtension: ~

# the slugger service needs a public alias for getting it from
# the container when loading doctrine fixtures
slugger:
alias: App\Utils\Slugger
public: true
8 changes: 4 additions & 4 deletions src/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function indexAction()
* to constraint the HTTP methods each controller responds to (by default
* it responds to all methods).
*/
public function newAction(Request $request, Slugger $slugger)
public function newAction(Request $request)
{
$post = new Post();
$post->setAuthor($this->getUser());
Expand All @@ -87,7 +87,7 @@ public function newAction(Request $request, Slugger $slugger)
// However, we explicitly add it to improve code readability.
// See https://symfony.com/doc/current/best_practices/forms.html#handling-form-submits
if ($form->isSubmitted() && $form->isValid()) {
$post->setSlug($slugger->slugify($post->getTitle()));
$post->setSlug(Slugger::slugify($post->getTitle()));

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

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

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

$this->addFlash('success', 'post.updated_successfully');
Expand Down
8 changes: 3 additions & 5 deletions src/DataFixtures/ORM/PostFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
use App\DataFixtures\FixturesTrait;
use App\Entity\Comment;
use App\Entity\Post;
use App\Utils\Slugger;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

/**
* Defines the sample blog posts to load in the database before running the unit
Expand All @@ -32,9 +31,8 @@
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class PostFixtures extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface
class PostFixtures extends AbstractFixture implements DependentFixtureInterface
{
use ContainerAwareTrait;
use FixturesTrait;

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

$post->setTitle($title);
$post->setSummary($this->getRandomPostSummary());
$post->setSlug($this->container->get('slugger')->slugify($post->getTitle()));
$post->setSlug(Slugger::slugify($post->getTitle()));
$post->setContent($this->getPostContent());
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));

Expand Down
5 changes: 1 addition & 4 deletions src/Utils/Slugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
namespace App\Utils;

/**
* This class is used to provide an example of integrating simple classes as
* services into a Symfony application.
*
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
Expand All @@ -25,7 +22,7 @@ class Slugger
*
* @return string
*/
public function slugify($string)
public static function slugify($string)
{
return preg_replace('/\s+/', '-', mb_strtolower(trim(strip_tags($string)), 'UTF-8'));
}
Expand Down
6 changes: 6 additions & 0 deletions src/Utils/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

namespace App\Utils;

/**
* This class is used to provide an example of integrating simple classes as
* services into a Symfony application.
*
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class Validator
{
public function validateUsername($username)
Expand Down
5 changes: 1 addition & 4 deletions tests/Utils/SluggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ class SluggerTest extends \PHPUnit_Framework_TestCase
*/
public function testSlugify($string, $slug)
{
$slugger = new Slugger();
$result = $slugger->slugify($string);

$this->assertSame($slug, $result);
$this->assertSame($slug, Slugger::slugify($string));
}

public function getSlugs()
Expand Down

0 comments on commit 41a5c0f

Please sign in to comment.