Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Removed Wordpress class dependency with KernelInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
xavismeh committed Jan 20, 2016
1 parent 74ebed8 commit 2e49b42
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 67 deletions.
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->scalarNode('table_prefix')->end()
->scalarNode('wordpress_directory')->end()
->scalarNode('wordpress_directory')->defaultNull()->end()
->scalarNode('entity_manager')->end()
->booleanNode('load_twig_extension')->defaultFalse()->end()
->booleanNode('cookie_hash')->defaultNull()->end()
->scalarNode('i18n_cookie_name')->defaultFalse()->end()
->booleanNode('enable_wordpress_listener')->defaultTrue()->end()

->arrayNode('globals')
->prototype('scalar')->end()
->prototype('scalar')->defaultValue([])->end()
->end()

->arrayNode('security')
Expand Down
29 changes: 4 additions & 25 deletions DependencyInjection/EkinoWordpressExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);

$container->setParameter('ekino.wordpress.install_directory', $config['wordpress_directory'] ?: $container->getParameter('kernel.root_dir').'/../../');
$this->loadWordpressGlobals($container, $config['globals']);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('manager.xml');
$loader->load('services.xml');
Expand All @@ -66,10 +69,6 @@ public function load(array $configs, ContainerBuilder $container)
$this->loadTablePrefix($container, $config['table_prefix']);
}

if (isset($config['wordpress_directory'])) {
$this->loadWordpressDirectory($container, $config['wordpress_directory']);
}

if (isset($config['entity_manager'])) {
$this->loadEntityManager($container, $config['entity_manager']);
}
Expand All @@ -87,10 +86,6 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('listener.xml');
}

if (isset($config['globals'])) {
$this->loadWordpressGlobals($container, $config['globals']);
}

$container->setParameter('ekino.wordpress.cookie_hash', $config['cookie_hash']);
$container->setParameter('ekino.wordpress.firewall_name', $config['security']['firewall_name']);
$container->setParameter('ekino.wordpress.login_url', $config['security']['login_url']);
Expand Down Expand Up @@ -135,22 +130,6 @@ protected function loadTablePrefix(ContainerBuilder $container, $prefix)
$container->setDefinition($identifier, $serviceDefinition);
}

/**
* Loads Wordpress directory from configuration.
*
* @param ContainerBuilder $container Symfony dependency injection container
* @param string $directory Wordpress directory
*/
protected function loadWordpressDirectory(ContainerBuilder $container, $directory)
{
$identifier = 'ekino.wordpress.wordpress';

$serviceDefinition = $container->getDefinition($identifier);
$serviceDefinition->addArgument($directory);

$container->setDefinition($identifier, $serviceDefinition);
}

/**
* Sets Doctrine entity manager for Wordpress.
*
Expand All @@ -177,7 +156,7 @@ protected function loadWordpressGlobals(ContainerBuilder $container, $globals)
$coreGlobals = ['wp', 'wp_the_query', 'wpdb', 'wp_query', 'allowedentitynames'];
$globals = array_merge($globals, $coreGlobals);

$container->findDefinition('ekino.wordpress.wordpress')->replaceArgument(1, $globals);
$container->setParameter('ekino.wordpress.globals', $globals);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<!-- Services -->

<service id="ekino.wordpress.wordpress" class="Ekino\WordpressBundle\Wordpress\Wordpress">
<argument type="service" id="kernel" />
<argument />
<argument>%ekino.wordpress.install_directory%</argument>
<argument>%ekino.wordpress.globals%</argument>
</service>

<!-- Subscribers -->
Expand Down
18 changes: 2 additions & 16 deletions Tests/Wordpress/WordpressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testExceptionWhenDirectoryNotFound()
{
$this->setExpectedException('InvalidArgumentException');

$wordpress = new Wordpress($this->getKernelMock(), ['wp_test_global1', 'wp_test_global2'], '/a/path/that/does/not/exists');
$wordpress = new Wordpress('/a/path/that/does/not/exists', ['wp_test_global1', 'wp_test_global2']);
$wordpress->initialize();
}

Expand All @@ -104,20 +104,6 @@ public function testGlobalVariables()
*/
protected function getWordpressMock()
{
$kernel = $this->getKernelMock();

return $this->getMock('\Ekino\WordpressBundle\Wordpress\Wordpress', ['getContent'], [$kernel, ['wp_test_global1', 'wp_test_global2']]);
}

/**
* Returns a mock of Symfony kernel.
*
* @return \Symfony\Component\HttpKernel\Kernel
*/
protected function getKernelMock()
{
return $this->getMockBuilder('\Symfony\Component\HttpKernel\Kernel')
->disableOriginalConstructor()
->getMock();
return $this->getMock('\Ekino\WordpressBundle\Wordpress\Wordpress', ['getContent'], [__DIR__, ['wp_test_global1', 'wp_test_global2']]);
}
}
29 changes: 8 additions & 21 deletions Wordpress/Wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace Ekino\WordpressBundle\Wordpress;

use Symfony\Component\HttpKernel\KernelInterface;

/**
* Class Wordpress.
*
Expand All @@ -23,20 +21,15 @@
class Wordpress
{
/**
* @var KernelInterface
* @var string
*/
protected $kernel;
protected $wordpressDirectory;

/**
* @var array
*/
protected $globals;

/**
* @var string
*/
protected $directory;

/**
* @var WordpressResponse
*/
Expand All @@ -45,20 +38,16 @@ class Wordpress
/**
* @var bool
*/
protected $alreadyInitialized;
protected $alreadyInitialized = false;

/**
* Constructor.
*
* @param KernelInterface $kernel Symfony kernel instance
* @param array $globals A Wordpress global variables array
* @param string $directory A wordpress directory (if specified in configuration)
* @param string $wordpressDirectory The wordpress directory installation
* @param array $globals A Wordpress global variables array
*/
public function __construct(KernelInterface $kernel, array $globals, $directory = null)
public function __construct($wordpressDirectory, array $globals)
{
$this->kernel = $kernel;
$this->globals = $globals;
$this->directory = $directory;
$this->wordpressDirectory = $wordpressDirectory;
}

/**
Expand Down Expand Up @@ -154,8 +143,6 @@ public function getWpQuery()
*/
protected function getWordpressDirectory()
{
$directory = $this->directory ?: sprintf('%s/../../', $this->kernel->getRootDir());

return '/' == substr($directory, -1) ? $directory : $directory.'/';
return '/' == substr($this->wordpressDirectory, -1) ? $this->wordpressDirectory : $this->wordpressDirectory.'/';
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ekino/wordpress-bundle",
"type": "library",
"type": "symfony-bundle",
"description": "A Symfony bundle to link Symfony with Wordpress",
"keywords": ["ekino", "bundle", "symfony", "wordpress"],
"homepage": "http://github.com/ekino/EkinoWordpressBundle",
Expand Down

0 comments on commit 2e49b42

Please sign in to comment.