Skip to content

Commit

Permalink
First commit for A brief eZ Publish Symfony stack training
Browse files Browse the repository at this point in the history
  • Loading branch information
Jani Tarvainen committed Dec 9, 2014
1 parent 005bb05 commit 768dab1
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Janit/EzXmlSitemapBundle/Controller/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Janit\EzXmlSitemapBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;

class DefaultController extends Controller
{

public function simpleSitemapAction()
{

$response = new Response();
$response->setPublic();
$response->setSharedMaxAge( 3600 );
$response->headers->set('Content-Type', 'text/xml');

$rootLocationId = $this->getConfigResolver()->getParameter( 'content.tree_root.location_id' );

$repository = $this->container->get( 'ezpublish.api.repository' );

$rootLocation = $repository->getLocationService()->loadLocation($rootLocationId);

$criteria = array(
new Criterion\Subtree($rootLocation->pathString),
new Criterion\Visibility( Criterion\Visibility::VISIBLE )
);

if ( !empty( $criterion ) )
$criteria[] = $criterion;

$query = new LocationQuery(
array(
'criterion' => new Criterion\LogicalAnd( $criteria )
)
);

$query->limit = 1000;

$searchService = $repository->getSearchService();

$searchResults = $searchService->findLocations($query);

return $this->render(
'JanitEzXmlSitemapBundle::sitemap.xml.twig',
array(
'searchHits' => $searchResults->searchHits,
),
$response
);

}

}
29 changes: 29 additions & 0 deletions Janit/EzXmlSitemapBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Janit\EzXmlSitemapBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('janit_ez_xml_sitemap');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.

return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Janit\EzXmlSitemapBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class JanitEzXmlSitemapExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
9 changes: 9 additions & 0 deletions Janit/EzXmlSitemapBundle/JanitEzXmlSitemapBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Janit\EzXmlSitemapBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class JanitEzXmlSitemapBundle extends Bundle
{
}
3 changes: 3 additions & 0 deletions Janit/EzXmlSitemapBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
janit_ez_xml_sitemap:
path: /sitemap.xml
defaults: { _controller: JanitEzXmlSitemapBundle:Default:simpleSitemap }
4 changes: 4 additions & 0 deletions Janit/EzXmlSitemapBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
# janit_ez_xml_sitemap.example:
# class: Janit\EzXmlSitemapBundle\Example
# arguments: [@service_id, "plain_value", %parameter%]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello {{ name }}!
9 changes: 9 additions & 0 deletions Janit/EzXmlSitemapBundle/Resources/views/sitemap.xml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

{% for location in searchHits %}
<url>
<loc>{{ app.request.getSchemeAndHttpHost() ~ path( 'ez_urlalias', {'locationId': location.valueObject.id} ) }}</loc>
</url>
{% endfor %}
</urlset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Janit\EzXmlSitemapBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DefaultControllerTest extends WebTestCase
{
public function testIndex()
{
$client = static::createClient();

$crawler = $client->request('GET', '/hello/Fabien');

$this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0);
}
}

0 comments on commit 768dab1

Please sign in to comment.