Skip to content

Commit

Permalink
Merge pull request #32 from Padam87/3.0
Browse files Browse the repository at this point in the history
3.0
  • Loading branch information
Padam87 committed Jul 16, 2015
2 parents e7836bd + 31319e9 commit 550079d
Show file tree
Hide file tree
Showing 41 changed files with 941 additions and 586 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/*
.idea
composer.lock
build
composer.lock
Tests/App/app/cache
49 changes: 49 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -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',
]
)
;
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6

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

before_script:
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
- composer update --dev
- composer update

script: phpunit

Expand All @@ -21,4 +21,4 @@ after_script:

notifications:
email:
- [email protected]
- [email protected]
2 changes: 1 addition & 1 deletion Annotation/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/
class Entity
{
}
}
103 changes: 103 additions & 0 deletions CacheWarmer/EntityAnnotationCacheWarmer.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();
}
}
59 changes: 0 additions & 59 deletions Controller/SchemaController.php

This file was deleted.

21 changes: 21 additions & 0 deletions DependencyInjection/Padam87AttributeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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;

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

$loader->load('services.yml');
}
}
Loading

0 comments on commit 550079d

Please sign in to comment.