Skip to content
Permalink
Browse files

First commit for A brief eZ Publish Symfony stack training

  • Loading branch information...
Jani Tarvainen
Jani Tarvainen committed Dec 9, 2014
1 parent 005bb05 commit 768dab1e1a0da70f7552307aff2d207d9d4b410f
@@ -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
);
}
}
@@ -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;
}
}
@@ -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');
}
}
@@ -0,0 +1,9 @@
<?php
namespace Janit\EzXmlSitemapBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class JanitEzXmlSitemapBundle extends Bundle
{
}
@@ -0,0 +1,3 @@
janit_ez_xml_sitemap:
path: /sitemap.xml
defaults: { _controller: JanitEzXmlSitemapBundle:Default:simpleSitemap }
@@ -0,0 +1,4 @@
services:
# janit_ez_xml_sitemap.example:
# class: Janit\EzXmlSitemapBundle\Example
# arguments: [@service_id, "plain_value", %parameter%]
@@ -0,0 +1 @@
Hello {{ name }}!
@@ -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>
@@ -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.
You can’t perform that action at this time.