Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CS-5549 - A new way to get theme path - speed improvement (issue serv…
…ice, listener, specs)
- Loading branch information
Showing
9 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* @package Newscoop | ||
* @copyright 2014 Sourcefabric z.ú. | ||
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt | ||
*/ | ||
|
||
namespace Newscoop; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Issue service interface | ||
*/ | ||
interface IssueServiceInterface | ||
{ | ||
/** | ||
* Issue resolver | ||
* | ||
* @param Request $request Request | ||
* | ||
* @return Issue|void Returns current issue or nothing | ||
*/ | ||
public function issueResolver(Request $request); | ||
|
||
/** | ||
* Get issue meta data | ||
* | ||
* @return array Issue meta data | ||
*/ | ||
public function getIssueMetadata(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* @package Newscoop | ||
* @copyright 2014 Sourcefabric z.ú. | ||
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt | ||
*/ | ||
|
||
namespace Newscoop\Services; | ||
|
||
use Newscoop\IssueServiceInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Doctrine\ORM\EntityManager; | ||
|
||
/** | ||
* Issue service | ||
*/ | ||
class IssueService implements IssueServiceInterface | ||
{ | ||
/** | ||
* Entity Manager | ||
* | ||
* @var EntityManager | ||
*/ | ||
protected $em; | ||
|
||
/** | ||
* Issue meta data array | ||
* | ||
* @var array | ||
*/ | ||
protected $issueMetadata = array(); | ||
|
||
/** | ||
* Publication service | ||
* | ||
* @var PublicationService | ||
*/ | ||
protected $publicationService; | ||
|
||
/** | ||
* Construct | ||
*/ | ||
public function __construct(EntityManager $em, PublicationService $publicationService) | ||
{ | ||
$this->em = $em; | ||
$this->publicationService = $publicationService; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getIssueMetadata() | ||
{ | ||
return $this->issueMetadata; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function issueResolver(Request $request) | ||
{ | ||
$uriParts = explode('/', $request->getRequestUri()); | ||
$publication = $this->publicationService->getPublication(); | ||
$issue = $this->em->getRepository('Newscoop\Entity\Issue')->findOneBy(array( | ||
'publication' => $publication, | ||
'shortName' => $uriParts[2] | ||
)); | ||
|
||
if ($issue) { | ||
$this->issueMetadata = array( | ||
'id' => $issue->getId(), | ||
'number' => $issue->getNumber(), | ||
'name' => $issue->getName(), | ||
'shortName' => $issue->getShortName() | ||
); | ||
|
||
$request->attributes->set('_newscoop_issue_metadata', $this->issueMetadata); | ||
|
||
return $issue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* @package Newscoop | ||
* @copyright 2014 Sourcefabric z.ú. | ||
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt | ||
*/ | ||
|
||
namespace Newscoop\Services; | ||
|
||
use Newscoop\ThemesServiceInterface; | ||
|
||
/** | ||
* Themes service | ||
*/ | ||
class ThemesService implements ThemesServiceInterface | ||
{ | ||
/** | ||
* Construct | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getThemePath() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* @package Newscoop | ||
* @copyright 2014 Sourcefabric z.ú. | ||
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org> | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt | ||
*/ | ||
|
||
namespace Newscoop; | ||
|
||
/** | ||
* Themes service interface | ||
*/ | ||
interface ThemesServiceInterface | ||
{ | ||
/** | ||
* Gets current theme path | ||
* | ||
* @return string Returns current theme path | ||
*/ | ||
public function getThemePath(); | ||
} |
49 changes: 49 additions & 0 deletions
49
newscoop/src/Newscoop/NewscoopBundle/EventListener/IssueListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* @package Newscoop\Newscoop | ||
* @author Rafał Muszyński <rafal.muszynski@sourcefabric.org> | ||
* @copyright 2014 Sourcefabric z.ú. | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt | ||
*/ | ||
|
||
namespace Newscoop\NewscoopBundle\EventListener; | ||
|
||
use Newscoop\Services\IssueService; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
|
||
/** | ||
* Issue listener to identify current issue | ||
*/ | ||
class IssueListener | ||
{ | ||
/** | ||
* Issue service | ||
* @var ThemesService | ||
*/ | ||
protected $issueService; | ||
|
||
/** | ||
* Contruct | ||
* | ||
* @param IssueService $issueService Issue service | ||
*/ | ||
public function __construct(IssueService $issueService) | ||
{ | ||
$this->issueService = $issueService; | ||
} | ||
|
||
/** | ||
* Resolve issue on request | ||
* | ||
* @param GetResponseEvent $event GetResponseEvent event | ||
* | ||
* @return void | ||
*/ | ||
public function onRequest(GetResponseEvent $event) | ||
{ | ||
$pos = strpos($event->getRequest()->getRequestUri(), 'admin'); | ||
if ($pos === false) { | ||
$this->issueService->issueResolver($event->getRequest()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
spec/Newscoop/NewscoopBundle/EventListener/IssueListenerSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace spec\Newscoop\NewscoopBundle\EventListener; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Newscoop\Services\IssueService; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class IssueListenerSpec extends ObjectBehavior | ||
{ | ||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Newscoop\NewscoopBundle\EventListener\IssueListener'); | ||
} | ||
|
||
public function let(IssueService $issueService, Request $request) | ||
{ | ||
$this->beConstructedWith($issueService); | ||
} | ||
|
||
public function it_calls_issue_resolver_on_request(GetResponseEvent $event, Request $request) | ||
{ | ||
$event->getRequest()->willReturn($request); | ||
$this->onRequest($event)->shouldReturn(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace spec\Newscoop\Services; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Newscoop\Entity\Issue; | ||
use Newscoop\Services\IssueService; | ||
use Newscoop\Services\PublicationService; | ||
use Doctrine\ORM\EntityManager; | ||
use Newscoop\Entity\Repository\IssueRepository; | ||
use Newscoop\Entity\Publication; | ||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
|
||
class IssueServiceSpec extends ObjectBehavior | ||
{ | ||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Newscoop\Services\IssueService'); | ||
$this->shouldImplement('Newscoop\IssueServiceInterface'); | ||
} | ||
|
||
public function let(EntityManager $em, PublicationService $publicationService, IssueRepository $repository, Issue $issue, Publication $publication) | ||
{ | ||
$em | ||
->getRepository('Newscoop\Entity\Issue') | ||
->willReturn($repository); | ||
|
||
$publicationService->getPublication()->willReturn($publication); | ||
|
||
$repository->findOneBy(array( | ||
'publication' => $publication, | ||
'shortName' => 'may2014' | ||
))->willReturn($issue); | ||
|
||
$this->beConstructedWith($em, $publicationService); | ||
} | ||
|
||
public function it_resolves_issue_from_request_data(Request $request, Issue $issue, ParameterBag $attributes) | ||
{ | ||
$issue->getId()->willReturn(1); | ||
$issue->getNumber()->willReturn(10); | ||
$issue->getName()->willReturn("May 2014"); | ||
$issue->getShortName()->willReturn("may2014"); | ||
$request->getRequestUri()->willReturn('/en/may2014/60/test-article.htm'); | ||
$request->attributes = $attributes; | ||
$request->attributes->set('_newscoop_issue_metadata', array( | ||
'id' => $issue->getId(), | ||
'number' => $issue->getNumber(), | ||
'name' => $issue->getName(), | ||
'shortName' => $issue->getShortName() | ||
)); | ||
|
||
$this->issueResolver($request)->shouldReturn($issue); | ||
} | ||
|
||
public function it_gets_current_issue_meta_data(ParameterBag $attributes, Issue $issue) | ||
{ | ||
$this->getIssueMetadata()->shouldReturn(array()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace spec\Newscoop\Services; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class ThemesServiceSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Newscoop\Services\ThemesService'); | ||
} | ||
} |