Commit 5b01fbc0 authored by Avris's avatar Avris

init

parents
<?php
namespace Avris\Micrus\Doctrine;
use Avris\Micrus\ORM;
use Avris\Micrus\App;
use Avris\Micrus\Doctrine\MicrusRepositoryFactory;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
class Doctrine implements ORM
{
/** @var EntityManager */
protected $entityManager;
public function __construct(App $app, $db)
{
$app->getLogger()->log('info', sprintf('Database driver %s', $db['driver']));
$config = Setup::createAnnotationMetadataConfiguration(
array($app->getRootDir() . '/App/Model'),
$app->getEnv() == 'dev',
null,
null,
false
);
$config->setRepositoryFactory(new MicrusRepositoryFactory());
$this->entityManager = EntityManager::create($db, $config);
}
public function getEntityManager()
{
return $this->entityManager;
}
public function find($type, $id)
{
return $this->entityManager->getRepository($type)->find($id);
}
public function findAll($type)
{
return $this->entityManager->getRepository($type)->findAll();
}
public function findBy($type, $attribute, $value)
{
return $this->entityManager->getRepository($type)->findBy(array($attribute => $value));
}
public function findOneBy($type, $attribute, $value)
{
return $this->entityManager->getRepository($type)->findOneBy(array($attribute => $value));
}
}
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Avris\Micrus\Doctrine;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
class MicrusRepositoryFactory extends DefaultRepositoryFactory
{
/**
* {@inheritDoc}
*/
protected function createRepository(EntityManagerInterface $entityManager, $entityName)
{
if (strpos($entityName, '\\') === false) {
$entityName = 'App\\Model\\' . ucfirst($entityName);
}
return parent::createRepository($entityManager, $entityName);
}
}
\ No newline at end of file
<?php
namespace App\Task;
use Avris\Micrus\Task\Task;
use Avris\Micrus\IO\ArgvInput;
use Avris\Micrus\IO\StdInput;
use Avris\Micrus\IO\StdOutput;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
class DoctrineTask extends Task {
public function configure()
{
$this
->addArgument('task')
->addOption('force')
;
}
public function execute(ArgvInput $argv, StdInput $input, StdOutput $output)
{
/** @var EntityManager $em */
$em = $this->getEm();
$schemaTool = new SchemaTool($em);
$metadatas = $em->getMetadataFactory()->getAllMetadata();
$conn = $em->getConnection();
$force = $argv->getOption('force');
if ($this->getApp()->getEnv() != 'dev' && $force) {
$output->writeln('This command can only be executed in dev environment.',
StdOutput::FOREGROUND_WHITE,
StdOutput::BACKGROUND_RED
);
return;
}
switch (strtolower($argv->getArgument('task'))) {
case 'create':
$sqls = $schemaTool->getCreateSchemaSql($metadatas);
break;
case 'update':
$sqls = $schemaTool->getUpdateSchemaSql($metadatas, true);
break;
case 'drop':
$sqls = $schemaTool->getDropSchemaSQL($metadatas);
break;
default:
$output->writeln('Unrecognised command.', StdOutput::FOREGROUND_WHITE, StdOutput::BACKGROUND_RED);
$output->writeln('Available command are: ');
$output->writeln(' * create');
$output->writeln(' * update');
$output->writeln(' * drop');
return;
}
if (!count($sqls)) {
$output->writeln('No queries to execute');
return;
}
if (!$force) {
$output->writeln(implode(';' . PHP_EOL, $sqls) . ';');
$output->writeln();
$output->writeln('No action will be actually taken, until this command is run with --force option.',
StdOutput::FOREGROUND_BLACK,
StdOutput::BACKGROUND_GREEN
);
$output->writeln();
return;
}
$output->writeln(sprintf('Executing %s queries', count($sqls)));
foreach ($sqls as $sql) {
$conn->executeQuery($sql);
}
$output->writeln('Queries were executed successfully');
}
}
\ No newline at end of file
{
"name": "avris/micrus-orm-doctrine",
"type": "library",
"description": "Doctrine ORM bridge for the Micrus framework",
"keywords": ["orm","doctrine","php","micrus","database"],
"license": "CC-BY",
"homepage": "http://micrus.avris.it",
"authors": [{
"name": "avris",
"email": "andrzej@avris.it"
}],
"require": {
"avris/micrus": "^1.1",
"doctrine/orm": "2.4.*"
},
"autoload": {
"psr-4": { "Avris\\Micrus\\ORM\\": "" }
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
\ No newline at end of file
services:
orm:
class: Avris\Micrus\Doctrine\Doctrine
parameters: [@app, @config.parameters.database]
tasks:
doctrine: Avris\Micrus\Doctrine\Task
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment