Skip to content

Commit

Permalink
Merge pull request #2 from ipublikuj/real-nette3
Browse files Browse the repository at this point in the history
Feature: Fixing nette 3 support
  • Loading branch information
akadlec authored Nov 10, 2019
2 parents 600f77d + 7c57677 commit 64a5503
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 94 deletions.
21 changes: 21 additions & 0 deletions .travis.composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$version = getenv('NETTE');

if (!$version || $version == 'default') {
exit;
}

echo "Nette version " . $version . PHP_EOL;

$file = __DIR__ . '/composer.json';

$content = file_get_contents($file);

$composer = json_decode($content, TRUE);

$composer['require']['nette/di'] = $version;

$content = json_encode($composer);

file_put_contents($file, $content);
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ before_install:
- composer self-update

before_script:
- php .travis.composer.php
- composer install --no-interaction --prefer-source --dev

script:
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "ipub/doctrine-crud",
"type" : "library",
"description" : "Extension of Kdyby/Doctrine with CRUD",
"description" : "CRUD methods for Doctrine2 ORM",
"keywords" : [
"doctrine2",
"doctrine",
Expand All @@ -11,7 +11,9 @@
"framework",
"tools"
],

"homepage" : "https://github.com/iPublikuj/doctrine-crud",

"license" : [
"GPL-2.0",
"GPL-3.0"
Expand Down Expand Up @@ -57,13 +59,11 @@
"nette/safe-stream" : "~2.3",
"nette/tester" : "~2.3",

"nette/http" : "~2.4",

"tracy/tracy" : "~2.4",

"pds/skeleton" : "~1.0",

"kdyby/doctrine" : "^3.3"
"nettrine/orm" : "~0.3"
},

"autoload" : {
Expand Down
4 changes: 2 additions & 2 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This Doctrine CRUD extension brings you easier way how to **C**reate, **R**ead,

> NOTE: reading is not implemented, because it depends on project what you are programming.
> WARGING: This extension does not implement Doctrine functions into your project. You have to install some third-party extension like [Kdyby/Doctrine](https://github.com/kdyby/doctrine) or write your own implementation od Doctrine database system
> WARGING: This extension does not implement Doctrine functions into your project. You have to install some third-party extension or write your own implementation od Doctrine database system
## Installation

Expand All @@ -28,7 +28,7 @@ At first you have to create CRUD service for given entity. It can be easily crea
```neon
services:
yourEntityCrud:
factory: @doctrine.crud(Your\Cool\NameSpace\YourEntity)
factory: @doctrine.crud::create(Your\Cool\NameSpace\YourEntity)
```

Now CRUD service for given entity is created and now you can create a manager for this entity:
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Doctrine
# Doctrine CRUD

[![Build Status](https://img.shields.io/travis/iPublikuj/doctrine-crud.svg?style=flat-square)](https://travis-ci.org/iPublikuj/doctrine-crud)
[![Scrutinizer Code Coverage](https://img.shields.io/scrutinizer/coverage/g/iPublikuj/doctrine-crud.svg?style=flat-square)](https://scrutinizer-ci.com/g/iPublikuj/doctrine-crud/?branch=master)
Expand All @@ -7,7 +7,7 @@
[![Composer Downloads](https://img.shields.io/packagist/dt/ipub/doctrine-crud.svg?style=flat-square)](https://packagist.org/packages/ipub/doctrine-crud)
[![License](https://img.shields.io/packagist/l/ipub/doctrine-crud.svg?style=flat-square)](https://packagist.org/packages/ipub/doctrine-crud)

Doctrine extension is here to extend [Kdyby/Doctrine](https://github.com/Kdyby/Doctrine) with CRUD system.
Implementation of CRUD system into [Doctrine3](https://github.com/doctrine/orm) with CRUD system.

## Installation

Expand Down
12 changes: 6 additions & 6 deletions src/IPub/DoctrineCrud/Crud/EntityCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,26 @@ final class EntityCrud implements IEntityCrud
private $entityMapper;

/**
* @var Create\IEntityCreator
* @var Crud\Create\IEntityCreator
*/
private $entityCreatorFactory;

/**
* @var Update\IEntityUpdater
* @var Crud\Update\IEntityUpdater
*/
private $entityUpdaterFactory;

/**
* @var Delete\IEntityDeleter
* @var Crud\Delete\IEntityDeleter
*/
private $entityDeleterFactory;

/**
* @param string $entityName
* @param Mapping\IEntityMapper $entityMapper
* @param Create\IEntityCreator $entityCreatorFactory
* @param Update\IEntityUpdater $entityUpdaterFactory
* @param Delete\IEntityDeleter $entityDeleterFactory
* @param Crud\Create\IEntityCreator $entityCreatorFactory
* @param Crud\Update\IEntityUpdater $entityUpdaterFactory
* @param Crud\Delete\IEntityDeleter $entityDeleterFactory
*/
public function __construct(
string $entityName,
Expand Down
4 changes: 2 additions & 2 deletions src/IPub/DoctrineCrud/Crud/EntityCrudFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public function __construct(
/**
* @param string $entityName
*
* @return IEntityCrud
* @return EntityCrud
*/
public function create(string $entityName) : IEntityCrud
public function create(string $entityName) : EntityCrud
{
return new EntityCrud(
$entityName,
Expand Down
103 changes: 66 additions & 37 deletions src/IPub/DoctrineCrud/DI/DoctrineCrudExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,43 +75,69 @@ public function loadConfiguration() : void
* CRUD factories
*/

$builder->addDefinition($this->prefix('entity.creator'))
->setType(Crud\Create\EntityCreator::class)
->setImplement(Crud\Create\IEntityCreator::class)
->setAutowired(FALSE);

$builder->addDefinition($this->prefix('entity.updater'))
->setType(Crud\Update\EntityUpdater::class)
->setImplement(Crud\Update\IEntityUpdater::class)
->setAutowired(FALSE);

$builder->addDefinition($this->prefix('entity.deleter'))
->setType(Crud\Delete\EntityDeleter::class)
->setImplement(Crud\Delete\IEntityDeleter::class)
->setAutowired(FALSE);

$builder->addDefinition($this->prefix('entity.crudFactory'))
->setType(Crud\EntityCrudFactory::class)
->setArguments([
'@' . $this->prefix('entity.mapper'),
'@' . $this->prefix('entity.creator'),
'@' . $this->prefix('entity.updater'),
'@' . $this->prefix('entity.deleter'),
]);

// Syntax sugar for config
$builder->addDefinition($this->prefix('crud'))
->setType(Crud\EntityCrud::class)
->setFactory('@IPub\DoctrineCrud\Crud\EntityCrudFactory::create', [new PhpGenerator\PhpLiteral('$entityName')])
->setParameters(['entityName'])
->setAutowired(FALSE);

/**
*
*/

$configuration = $builder->getDefinition('doctrine.default.ormConfiguration');
$configuration->addSetup('addCustomStringFunction', ['DATE_FORMAT', DoctrineCrud\StringFunctions\DateFormat::class]);
// NETTE 3.x
if (method_exists($builder, 'addFactoryDefinition')) {
$builder->addFactoryDefinition($this->prefix('entity.creator'))
->setImplement(Crud\Create\IEntityCreator::class)
->setAutowired(FALSE)
->getResultDefinition()
->setType(Crud\Create\EntityCreator::class);

$builder->addFactoryDefinition($this->prefix('entity.updater'))
->setImplement(Crud\Update\IEntityUpdater::class)
->setAutowired(FALSE)
->getResultDefinition()
->setFactory(Crud\Update\EntityUpdater::class);

$builder->addFactoryDefinition($this->prefix('entity.deleter'))
->setImplement(Crud\Delete\IEntityDeleter::class)
->setAutowired(FALSE)
->getResultDefinition()
->setFactory(Crud\Delete\EntityDeleter::class);

// Syntax sugar for config
$builder->addFactoryDefinition($this->prefix('crud'))
->setImplement(Crud\IEntityCrudFactory::class)
->setParameters(['entityName'])
->getResultDefinition()
->setType(Crud\EntityCrud::class)
->setArguments([
new PhpGenerator\PhpLiteral('$entityName'),
'@' . $this->prefix('entity.mapper'),
'@' . $this->prefix('entity.creator'),
'@' . $this->prefix('entity.updater'),
'@' . $this->prefix('entity.deleter'),
]);

// NETTE 2.4+
} else {
$builder->addDefinition($this->prefix('entity.creator'))
->setType(Crud\Create\EntityCreator::class)
->setImplement(Crud\Create\IEntityCreator::class)
->setAutowired(FALSE);

$builder->addDefinition($this->prefix('entity.updater'))
->setType(Crud\Update\EntityUpdater::class)
->setImplement(Crud\Update\IEntityUpdater::class)
->setAutowired(FALSE);

$builder->addDefinition($this->prefix('entity.deleter'))
->setType(Crud\Delete\EntityDeleter::class)
->setImplement(Crud\Delete\IEntityDeleter::class)
->setAutowired(FALSE);

$builder->addDefinition($this->prefix('crud'))
->setType(Crud\EntityCrud::class)
->setImplement(Crud\IEntityCrudFactory::class)
->setParameters(['entityName'])
->setArguments([
new PhpGenerator\PhpLiteral('$entityName'),
'@' . $this->prefix('entity.mapper'),
'@' . $this->prefix('entity.creator'),
'@' . $this->prefix('entity.updater'),
'@' . $this->prefix('entity.deleter'),
]);
}
}

/**
Expand All @@ -131,6 +157,9 @@ public function beforeCompile() : void
// Register validator to proxy validator
$validator->addSetup('registerValidator', ['@' . $serviceName, $serviceName]);
}

$builder->getDefinition($builder->getByType('Doctrine\ORM\EntityManagerInterface', TRUE))
->addSetup('?->getConfiguration()->addCustomStringFunction(?, ?)', ['@self', 'DATE_FORMAT', DoctrineCrud\StringFunctions\DateFormat::class]);
}

/**
Expand Down
20 changes: 13 additions & 7 deletions tests/IPubTests/DoctrineCrud/CRUDTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ namespace IPubTests\DoctrineCrud;
use Nette;
use Nette\Utils;

use Kdyby;
use Doctrine\ORM;

use Nettrine;

use Tester;
use Tester\Assert;

use Doctrine\ORM;

use IPubTests\DoctrineCrud\Models;

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'bootstrap.php';
Expand All @@ -51,7 +51,7 @@ class CRUDTest extends Tester\TestCase
private $container;

/**
* @var Kdyby\Doctrine\EntityManager
* @var ORM\EntityManager
*/
private $em;

Expand All @@ -68,8 +68,8 @@ class CRUDTest extends Tester\TestCase
parent::setUp();

$this->container = $this->createContainer();
$this->em = $this->container->getByType('Kdyby\Doctrine\EntityManager');
$this->manager = $this->container->getByType('IPubTests\DoctrineCrud\Models\UsersManager');
$this->em = $this->container->getByType(Nettrine\ORM\EntityManagerDecorator::class);
$this->manager = $this->container->getByType(Models\UsersManager::class);
}

public function testCreateEntity() : void
Expand Down Expand Up @@ -252,7 +252,13 @@ class CRUDTest extends Tester\TestCase
$config->addParameters(['appDir' => $rootDir, 'wwwDir' => $rootDir]);

$config->addConfig(__DIR__ . DS . 'files' . DS . 'config.neon');
$config->addConfig(__DIR__ . DS . 'files' . DS . 'entities.neon');

if (getenv('NETTE') === 'default') {
$config->addConfig(__DIR__ . DS . 'files' . DS . 'entities.neon');

} else {
$config->addConfig(__DIR__ . DS . 'files' . DS . 'entities24.neon');
}

return $config->createContainer();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/IPubTests/DoctrineCrud/ExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class ExtensionTest extends Tester\TestCase
{
$dic = $this->createContainer();

$crudFactory = $dic->getByType('IPub\DoctrineCrud\Crud\EntityCrudFactory');
Assert::true($crudFactory instanceof DoctrineCrud\Crud\EntityCrudFactory);
$crudFactory = $dic->getByType(DoctrineCrud\Crud\IEntityCrudFactory::class);
Assert::true($crudFactory instanceof DoctrineCrud\Crud\IEntityCrudFactory);
}

/**
Expand Down
38 changes: 13 additions & 25 deletions tests/IPubTests/DoctrineCrud/files/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,20 @@ php:
date.timezone: Europe/Prague

extensions:
doctrine: Kdyby\Doctrine\DI\OrmExtension
dbal: Nettrine\DBAL\DI\DbalExtension
orm: Nettrine\ORM\DI\OrmExtension
orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension
orm.cache: Nettrine\ORM\DI\OrmCacheExtension
doctrineCrud: IPub\DoctrineCrud\DI\DoctrineCrudExtension
annotations: Kdyby\Annotations\DI\AnnotationsExtension
console: Kdyby\Console\DI\ConsoleExtension
events: Kdyby\Events\DI\EventsExtension

services:
cacheStorage:
class: Nette\Caching\Storages\MemoryStorage
dbal:
connection:
host: 127.0.0.1
driver: pdo_sqlite
memory: true

annotations:
cache: array
orm.annotations:
defaultCache: array

console:
url: http://example.com

doctrine:
metadataCache: array
queryCache: array
resultCache: array
hydrationCache: array
driver: pdo_sqlite
memory: true

http:
frames: null

session:
autoStart: false
orm.cache:
defaultDriver: array
16 changes: 9 additions & 7 deletions tests/IPubTests/DoctrineCrud/files/entities.neon
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
services:
userCrud:
factory: @doctrineCrud.crud(IPubTests\DoctrineCrud\Models\UserEntity)
factory: @doctrineCrud.crud::create(IPubTests\DoctrineCrud\Models\UserEntity)

articleCrud:
factory: @doctrineCrud.crud(IPubTests\DoctrineCrud\Models\ArticleEntity)
factory: @doctrineCrud.crud::create(IPubTests\DoctrineCrud\Models\ArticleEntity)

usersManager:
class: IPubTests\DoctrineCrud\Models\UsersManager(@userCrud)
factory: IPubTests\DoctrineCrud\Models\UsersManager(@userCrud)

articlesManager:
class: IPubTests\DoctrineCrud\Models\ArticlesManager(@articleCrud)
factory: IPubTests\DoctrineCrud\Models\ArticlesManager(@articleCrud)

doctrine:
metadata:
IPubTests\DoctrineCrud: %appDir%IPubTests/DoctrineCrud/models
orm.annotations:
paths:
- %appDir%IPubTests/DoctrineCrud/models
Loading

0 comments on commit 64a5503

Please sign in to comment.