Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Baldur Rensch committed Jul 26, 2013
1 parent 0db4d2f commit 3bbfae2
Show file tree
Hide file tree
Showing 14 changed files with 466 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Symfony directories
vendor/*

composer.lock
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php
php:
- 5.3
- 5.4
- 5.5

before_script:
- composer install --prefer-source --no-interaction

script:
- phpunit -c phpunit.xml.dist --coverage-text

notifications:
email: false
74 changes: 74 additions & 0 deletions Alice/DataFixtureLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Hautelook\AliceBundle\Alice;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class DataFixtureLoader
*
* @author Baldur Rensch <brensch@gmail.com>
*/
abstract class DataFixtureLoader implements FixtureInterface, ContainerAwareInterface
{
/**
* @var ObjectManager
*/
protected $manager;

/**
* @var ContainerInterface
*/
protected $container;

/**
* Load data fixtures with the passed EntityManager
*
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager)
{
$this->manager = $manager;
/** @var $loader \Hautelook\AliceBundle\Alice\Loader */
$loader = $this->container->get('hautelook_alice.loader');
$loader->setObjectManager($manager);
$loader->setProviders(array($this));

$loader->load($this->getFixtures());
}

/**
* Returns an array of file paths to fixtures
*
* @return array<string>
*/
abstract protected function getFixtures();

/**
* Sets the Container.
*
* @param ContainerInterface|null $container A ContainerInterface instance or null
*
* @api
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* Find Entity by primary key(s)
*
* @param string $className
* @param string|array<string> $ids
*
* @return object
*/
public function find($className, $ids)
{
return $this->manager->find($className, $ids);
}
}
33 changes: 33 additions & 0 deletions Alice/Doctrine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Hautelook\AliceBundle\Alice;

use Nelmio\Alice\ORM\Doctrine as BaseDoctrine;

/**
* Class Doctrine - Adapter for Doctrine ORM
* @author Baldur Rensch <brensch@gmail.com>
*/
class Doctrine extends BaseDoctrine
{
/**
* Detaches an entity
*
* @param mixed $obj
*/
public function detach($obj)
{
$this->om->detach($obj);
}

/**
* Merges an entity
*
* @param $obj
*
* @return object
*/
public function merge($obj)
{
return $this->om->merge($obj);
}
}
133 changes: 133 additions & 0 deletions Alice/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Hautelook\AliceBundle\Alice;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Persistence\ObjectManager;
use Psr\Log\LoggerInterface;

/**
* Loader
* @author Baldur Rensch <brensch@gmail.com>
*/
class Loader
{
/**
* @var array
*/
private $providers;

/**
* @var array
*/
private $loaders;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var Doctrine
*/
private $persister;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var ArrayCollection
*/
private $references;

/**
* @param $loaders
* @param LoggerInterface $logger
*/
public function __construct($loaders, LoggerInterface $logger = null)
{
$this->loaders = $loaders;
$this->logger = $logger;
$this->references = new ArrayCollection();
}

/**
* @param ObjectManager $manager
*/
public function setObjectManager(ObjectManager $manager)
{
$this->objectManager = $manager;

$this->persister = new Doctrine($this->objectManager);

$newReferences = array();
foreach ($this->references as $name => $reference) {
$newReferences[$name] = $this->persister->merge($reference);
}
$this->references = new ArrayCollection($newReferences);

/** @var $loader \Nelmio\Alice\Loader\Base */
foreach ($this->loaders as $loader) {
$loader->setLogger($this->logger);
$loader->setORM($this->persister);
$loader->setReferences($newReferences);
}
}

/**
* @param array<string> $files
*/
public function load(array $files)
{
$objects = array();
foreach ($files as $file) {
/** @var $loader \Nelmio\Alice\Loader\Base */
$loader = $this->getLoader('yaml');
$loader->setProviders($this->providers);

$set = $loader->load($file);
$this->persister->persist($set);

$objects = array_merge($objects, $set);
}

foreach ($objects as $name => $obj) {
$this->persister->detach($obj);
$this->references->set($name, $obj);
}
}

/**
* @param array $providers
*/
public function setProviders(array $providers)
{
$this->providers = $providers;
}

/**
* @param string $key
*
* @throws \InvalidArgumentException
* @return mixed
*/
protected function getLoader($key)
{
if (empty($this->loaders[$key])) {
throw new \InvalidArgumentException("Unknown loader type: {$key}");
}
// if (is_string($file) && preg_match('{\.ya?ml(\.php)?$}', $file)) {
// $loader = self::getLoader('Yaml', $options);
// } elseif ((is_string($file) && preg_match('{\.php$}', $file)) || is_array($file)) {
// $loader = self::getLoader('Base', $options);
// } else {
// throw new \InvalidArgumentException('Unknown file/data type: '.gettype($file).' ('.json_encode($file).')');
// }
/** @var $loader \Nelmio\Alice\LoaderInterface */
$loader = $this->loaders[$key];

return $loader;
}
}
28 changes: 28 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Hautelook\AliceBundle\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('hautelook_alice');

// 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;
}
}
28 changes: 28 additions & 0 deletions DependencyInjection/HautelookAliceExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Hautelook\AliceBundle\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 HautelookAliceExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

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

namespace Hautelook\AliceBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class HautelookAliceBundle extends Bundle
{
}
29 changes: 29 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="hautelook_alice.loader.yaml.class">Nelmio\Alice\Loader\Yaml</parameter>
<parameter key="hautelook_alice.loader.class">Hautelook\AliceBundle\Alice\Loader</parameter>
</parameters>

<services>

<service id="hautelook_alice.loader.yaml" class="%hautelook_alice.loader.yaml.class%">
<argument>en_US</argument><!-- locale -->
<argument type="collection"/>
<argument></argument><!-- seed -->
</service>

<service id="hautelook_alice.loader" class="%hautelook_alice.loader.class%">
<argument type="collection">
<argument key="yaml" type="service" id="hautelook_alice.loader.yaml" />
</argument>
<argument type="service" id="logger" />
</service>

</services>

</container>
File renamed without changes.
Loading

0 comments on commit 3bbfae2

Please sign in to comment.