Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Feature/current language non url dependant #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ twig:
parameters: "@neko_wiki.provider.wiki_parameter"
language_manager: "@neko_wiki.language.manager"




# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
Expand Down
4 changes: 2 additions & 2 deletions features/homepage.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: Show the homepage

Scenario: homepage
When I am on "/en/article/home.html"
Then I should see "Home"
When I am on "/"
Then I should see "Home"
3 changes: 2 additions & 1 deletion spec/App/ParamConverter/PageParamConverterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function it_is_initializable()

public function let(PageProvider $pageProvider, Page $page)
{
$pageProvider->findPageBySlug(Argument::any())->willReturn($page);
$pageProvider->findPageBySlugAndCulture(Argument::any(), Argument::any())->willReturn($page);
$this->beConstructedWith($pageProvider);
}

Expand Down Expand Up @@ -48,6 +48,7 @@ public function it_should_return_a_page_when_apply(Request $request, ParameterBa
{
$request->attributes = $parameterBag;
$parameterBag->get(Argument::any())->willReturn('home');
$parameterBag->get(Argument::any(), Argument::any())->willReturn('fr');
$parameterBag->set('page', $page)->shouldBeCalled();

$this->apply($request, $configuration)->shouldReturn(true);
Expand Down
16 changes: 3 additions & 13 deletions spec/App/Provider/PageProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,12 @@ public function it_is_initializable()
$this->shouldHaveType('App\Provider\PageProvider');
}

public function let(
EntityManager $em,
EntityRepository $repo,
PageTranslation $translation,
Page $page,
RequestStack $requestStack,
Request $request
) {
$requestStack->getCurrentRequest()->willReturn($request);
$request->getLocale()->willReturn('en');

function let(EntityManager $em, EntityRepository $repo, PageTranslation $translation, Page $page)
{
$this->beConstructedWith($em);
$em->getRepository(Argument::any())->willReturn($repo);
$repo->findOneBy(Argument::any())->willReturn($translation);
$translation->getTranslatable()->willReturn($page);

$this->beConstructedWith($em, $requestStack);
}

public function its_getHomepage_that_should_return_page($page)
Expand Down
9 changes: 1 addition & 8 deletions src/App/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ public function languageAction(Request $request, $_locale = null)

$page = $this->get('neko_wiki.provider.page')->getHomepage();

return $this->redirectToRoute('show_page', ['page_slug' => $page->getTitleSlug(), '_locale' => $language]);
}

public function homeAction()
{
$page_provider = $this->get('neko_wiki.provider.page');

return $this->render('NekoWiki:Page:basic.html.twig', ['page' => $page_provider->getHomepage()]);
return $this->redirectToRoute('show_page', ['page_slug' => $page->getTitleSlug(), 'culture' => $language]);
}

public function footerAction()
Expand Down
3 changes: 2 additions & 1 deletion src/App/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public function newAction(Request $request)

/**
* Modify an existing page
* TODO: why the fuck the page parameter would be null ?
*
* @param Request $request
* @param Page $page
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Request $request, Page $page)
public function editAction(Request $request, Page $page = null)
{
$form = $this->createForm('neko_wiki_page', $page);
$form->handleRequest($request);
Expand Down
5 changes: 3 additions & 2 deletions src/App/ParamConverter/PageParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public function __construct(PageProvider $provider)
*/
public function apply(Request $request, ParamConverter $configuration)
{
$slug = $request->attributes->get('page_slug');
$page = $this->provider->findPageBySlug($slug);
$slug = $request->attributes->get('page_slug');
$culture = $request->attributes->get('culture', null);
$page = $this->provider->findPageBySlugAndCulture($slug, $culture);

$request->attributes->set('page', $page);

Expand Down
26 changes: 21 additions & 5 deletions src/App/Provider/PageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(EntityManager $em, RequestStack $requestStack)
*/
public function getHomepage()
{
$page = $this->findPageBySlug('home');
$page = $this->findPageBySlugAndCulture('home');

if ($page === null) {
throw new NotFoundHttpException('The homepage is not findable. Please read the documentation of NekoWiki about installation.');
Expand All @@ -38,16 +38,32 @@ public function getHomepage()

/**
* @param string $slug
* @param string $preferredCulture
* @return null|\App\Entity\Page
*/
public function findPageBySlug($slug)
public function findPageBySlugAndCulture($slug, $preferredCulture = null)
{
$translation = $this->getTranslationRepository()->findOneBy(['titleSlug' => $slug]);
if ($translation === null) {
$translation = null;
$criteria = ['titleSlug' => $slug];
$repository = $this->getTranslationRepository();

if (null === $preferredCulture) {
$translation = $repository->findOneBy($criteria);
} else {
$translation = $repository->findOneBy(array_merge(['locale' => $preferredCulture], $criteria));
if (null === $translation) {
$translation = $repository->findOneBy($criteria);
}
}

if (null === $translation) {
return null;
}

return $translation->getTranslatable();
$page = $translation->getTranslatable();
$page->setCurrentLocale($preferredCulture);

return $page;
}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/App/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ homepage:
defaults:
_controller: 'NekoWiki:Home:language'

homepage_by_lang:
path: /{_locale}/
defaults:
_controller: 'NekoWiki:Home:language'
neko_wiki_page:
prefix: /
resource: 'routing/page.yml'

neko_wiki_user:
prefix: /
resource: 'routing/user.yml'

neko_wiki_main:
prefix: /{_locale}
neko_wiki_global:
prefix: /
resource: 'routing/global.yml'
24 changes: 1 addition & 23 deletions src/App/Resources/config/routing/global.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# routing imports
neko_wiki_user:
resource: 'user.yml'


# no category routes

# No category routes
search:
path: /search.html
defaults:
Expand All @@ -18,19 +12,3 @@ search_results:
_controller: 'NekoWiki:Search:results'
requirements:
_method: GET


edit_page:
path: /article/{page_slug}/modify.html
defaults:
_controller: 'NekoWiki:Page:edit'

new_page:
path: /article/creation
defaults:
_controller: 'NekoWiki:Page:new'

show_page:
path: /article/{page_slug}.html
defaults:
_controller: 'NekoWiki:Page:show'
15 changes: 15 additions & 0 deletions src/App/Resources/config/routing/page.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
show_page:
path: /{culture}/{page_slug}.html
defaults:
_controller: 'NekoWiki:Page:show'

edit_page:
path: /{culture}/edit/{page_slug}.html
defaults:
_controller: 'NekoWiki:Page:edit'


#new_page:
# path: /article/creation
# defaults:
# _controller: 'NekoWiki:Page:new'
2 changes: 1 addition & 1 deletion src/App/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ app:
menu:
page: Page
modify: Modify
other_translations: Other languages
other_translations: In other languages
form:
add_translation: Add a translation
content: Content (%lang%)
Expand Down
2 changes: 1 addition & 1 deletion src/App/Resources/views/Page/_other_translations.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% for translation in translations %}
{% if languages[translation.locale] is defined %}
<li>
<a href="{{ path('show_page', {'_locale': translation.locale, 'page_slug': translation.titleSlug}) }}">
<a href="{{ path('show_page', {'culture': translation.locale, 'page_slug': translation.titleSlug}) }}">
{{ languages[translation.locale] | capitalize }}
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/App/Resources/views/page_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{% trans %}app.page.menu.page{% endtrans %} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<li><a href="{{ path('edit_page', {'page_slug': page.titleSlug, '_locale': page.locale}) }}">{% trans %}app.page.menu.modify{% endtrans %}</a></li>
<li><a href="{{ path('edit_page', {'page_slug': page.titleSlug, 'culture': language_manager.currentLanguage}) }}">{% trans %}app.page.menu.modify{% endtrans %}</a></li>
</ul>
</li>

Expand Down