Skip to content

Commit

Permalink
Merge 398e534 into fa3fa69
Browse files Browse the repository at this point in the history
  • Loading branch information
Padam87 committed Jul 15, 2015
2 parents fa3fa69 + 398e534 commit f82057e
Show file tree
Hide file tree
Showing 32 changed files with 703 additions and 424 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
vendor/*
.idea
build
composer.lock
Tests/App/app/cache
49 changes: 49 additions & 0 deletions .php_cs
@@ -0,0 +1,49 @@
<?php

return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::NONE_LEVEL)
->fixers(
[
// Symfony level
'psr0',
'encoding',
'short_tag',
'braces',
'elseif',
'eof_ending',
'function_declaration',
'indentation',
'line_after_namespace',
'linefeed',
'lowercase_constants',
'lowercase_keywords',
'multiple_use',
'php_closing_tag',
'trailing_spaces',
'visibility',
//'concat_without_spaces',
'duplicate_semicolon',
'extra_empty_lines',
'include',
'multiline_array_trailing_comma',
'namespace_no_leading_whitespace',
'new_with_braces',
'object_operator',
'operators_spaces',
'phpdoc_params',
'remove_lines_between_uses',
'return',
'single_array_no_trailing_comma',
'spaces_before_semicolon',
'spaces_cast',
'standardize_not_equal',
'ternary_spaces',
'unused_use',
'whitespacy_lines',
// Custom
'ordered_use',
'short_array_syntax',
'concat_with_spaces',
]
)
;
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -6,10 +6,9 @@ php:
- 5.6

env:
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION=2.4.*
- SYMFONY_VERSION=2.5.*
- SYMFONY_VERSION=2.6.*
- SYMFONY_VERSION=2.7.*

before_script:
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
Expand Down
2 changes: 1 addition & 1 deletion Annotation/Entity.php
Expand Up @@ -7,4 +7,4 @@
*/
class Entity
{
}
}
103 changes: 103 additions & 0 deletions CacheWarmer/EntityAnnotationCacheWarmer.php
@@ -0,0 +1,103 @@
<?php

namespace Padam87\AttributeBundle\CacheWarmer;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

class EntityAnnotationCacheWarmer implements CacheWarmerInterface
{
/**
* @var ManagerRegistry
*/
private $doctrine;

/**
* @var bool
*/
private $debug;

/**
* @param ManagerRegistry $doctrine
* @param $debug
*/
public function __construct(ManagerRegistry $doctrine, $debug)
{
$this->doctrine = $doctrine;
$this->debug = $debug;
}

/**
* {@inheritdoc}
*/
public function isOptional()
{
return false;
}

/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
{
$filename = $cacheDir . '/padam87/attribute_bundle/Entity.cache.php';

$cache = new ConfigCache($filename, $this->debug);

if (!$cache->isFresh()) {
$content = '<?php return ' . var_export($this->getEntities(), true) . ';';
$cache->write($content, $this->getResources());
}
}

protected function getEntities()
{
$entities = [];

$reader = new AnnotationReader();

/** @var ClassMetadata $metadata */
foreach ($this->getMetadata() as $metadata) {
$refl = $metadata->getReflectionClass();

if ($refl === null) {
$refl = new \ReflectionClass($metadata->getName());
}

if ($reader->getClassAnnotation($refl, 'Padam87\AttributeBundle\Annotation\Entity') != null) {
$entities[$metadata->getName()] = true;
}
}

return $entities;
}

/**
* @return array
*/
protected function getResources()
{
$res = [];

/** @var ClassMetadata $m */
foreach ($this->getMetadata() as $m) {
$res[] = new FileResource($m->getReflectionClass()->getFileName());
}

return $res;
}

/**
* @return array
*/
protected function getMetadata()
{
$em = $this->doctrine->getManager();

return $em->getMetadataFactory()->getAllMetadata();
}
}
77 changes: 77 additions & 0 deletions Command/SyncSchemaCommand.php
@@ -0,0 +1,77 @@
<?php

namespace Padam87\AttributeBundle\Command;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Padam87\AttributeBundle\Entity\Schema;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SyncSchemaCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('eav:schema:sync')
->setDescription('Syncs the existing schemas in the database with the current metadata')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$reader = new AnnotationReader();
/** @var ManagerRegistry $doctrine */
$doctrine = $this->getContainer()->get('doctrine');
$em = $doctrine->getManager();
$cmf = $em->getMetadataFactory();

$existing = [];
$created = [];

/** @var ClassMetadata $metadata */
foreach ($cmf->getAllMetadata() as $metadata) {
$refl = $metadata->getReflectionClass();

if ($refl === null) {
$refl = new \ReflectionClass($metadata->getName());
}

if ($reader->getClassAnnotation($refl, 'Padam87\AttributeBundle\Annotation\Entity') != null) {
$schema = $em->getRepository('Padam87AttributeBundle:Schema')->findOneBy([
'className' => $metadata->getName(),
]);

if ($schema === null) {
$schema = new Schema();
$schema->setClassName($metadata->getName());

$em->persist($schema);
$em->flush($schema);

$created[] = $metadata->getName();
} else {
$existing[] = $metadata->getName();
}
}
}

$table = new Table($output);

$table->addRow(['Created:', implode(PHP_EOL, $created)]);
$table->addRow(new TableSeparator());
$table->addRow(['Existing:', implode(PHP_EOL, $existing)]);

$table->render();
}
}
15 changes: 8 additions & 7 deletions DependencyInjection/Padam87AttributeExtension.php
Expand Up @@ -2,19 +2,20 @@

namespace Padam87\AttributeBundle\DependencyInjection;

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

class Padam87AttributeExtension extends Extension
{
public function load(array $config, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
);
$loader->load('services.yml');
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
);

$loader->load('services.yml');
}
}
30 changes: 14 additions & 16 deletions Entity/Attribute.php
Expand Up @@ -11,34 +11,38 @@
class Attribute
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @var int
*/
private $id;

/**
* @ORM\Column(type="string", length=255, nullable=true)
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $value;

/**
* @ORM\ManyToOne(targetEntity="Definition", inversedBy="attributes", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="definition_id", referencedColumnName="id")
* @var Definition
*
* @ORM\ManyToOne(targetEntity="Definition", inversedBy="attributes")
* @ORM\JoinColumn(name="definition_id", referencedColumnName="id")
*/
private $definition;

/**
* @return string
*/
public function __toString()
{
return $this->getDefinition()->getName();
}

/**
* Get id
*
* @return integer
*/
public function getId()
Expand All @@ -47,9 +51,8 @@ public function getId()
}

/**
* Set value
*
* @param string $value
*
* @return Attribute
*/
public function setValue($value)
Expand All @@ -60,8 +63,6 @@ public function setValue($value)
}

/**
* Get value
*
* @return string
*/
public function getValue()
Expand All @@ -70,22 +71,19 @@ public function getValue()
}

/**
* Set definition
* @param Definition $definition
*
* @param \Padam87\AttributeBundle\Entity\Definition $definition
* @return Attribute
*/
public function setDefinition(\Padam87\AttributeBundle\Entity\Definition $definition = null)
public function setDefinition(Definition $definition = null)
{
$this->definition = $definition;

return $this;
}

/**
* Get definition
*
* @return \Padam87\AttributeBundle\Entity\Definition
* @return Definition
*/
public function getDefinition()
{
Expand Down

0 comments on commit f82057e

Please sign in to comment.