Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Jul 12, 2019
1 parent 3fb7376 commit 2156368
Show file tree
Hide file tree
Showing 43 changed files with 4,132 additions and 0 deletions.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Baraja Doctrine database
========================

Simple easy to use, maximal performance database layer with connection to Doctrine and support for Nette 3.0.

This package install Doctrine automatically to your project with stable run.

How to install
--------------

Simple call Composer command:

```shell
composer require baraja/doctrine
```

In project `common.neon` you must define database credentials. Fully works example configuration is in `config.neon` file in this package.

This package support automatically install by PackageRegistrator. If you haven't, you should install manually by this manual.

All configuration you can define simply by parameters (stored in super-global array `parameters`).

For example:

```yaml
parameters:
database:
primary:
host: 127.0.0.1
dbname: sandbox
user: root
password: root
```

Package support to one database in specific moment now.

Best performance
----------------

When Doctrine is used poorly, it can be unnecessarily slow.

More details (Czech language): https://ondrej.mirtes.cz/doctrine-2-neni-pomala

This package use best-practices to performance increase. Set automatically `autoGenerateProxyClasses` to `false`, ProxyClasses will be generated when Doctrine needs.

For maximal performance is best save your generated meta data about entities to Redis: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/caching.html

UUID
----

For unique record (entity) identification package defines trait `UuidIdentifier` with defined all basic best-practice configuration for your entity. ID will be generated automatically.

For better experience please insert two traits to all entities in project:

```php
<?php

declare(strict_types=1);

namespace Baraja\Doctrine\Entity;


use Doctrine\ORM\Mapping as ORM;
use Nette\SmartObject;
use Baraja\Doctrine\UUID\UuidIdentifier;

/**
* @ORM\Entity()
*/
class DatabaseEntity
{

use UuidIdentifier; // <--- UUID trait for entity identifier.
use SmartObject; // <--- Strict class for better experience.
```

UUID will be generated automatically in PHP.

Manipulation with entities
--------------------------

Package defines DIC service `DoctrineHelper` with super useful methods.

- `getEntityVariants(string $entity, array $exclude = null): array`
- `getBestOfType(string $entity): string`
- `getTableNameByEntity(string $entity): string`
- `getRootEntityName(string $entity): string`
- `getDiscriminatorByEntity(string $entity): string`
- `remapEntityToBestType($from)`
- `remapEntity($from, $to)`



30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "baraja-core/doctrine",
"description": "Doctrine port to Nette 3.0 with maximal performance.",
"homepage": "https://github.com/baraja-core/doctrine",
"authors": [
{
"name": "Jan Barášek",
"homepage": "http://baraja.cz"
}
],
"require": {
"php": ">=7.1.0",
"ext-json": "*",
"ext-PDO": "*",
"ext-tokenizer": "*",
"symfony/console": "^4.2.3",
"contributte/console": "^0.6.0",
"nette/di": "^3.0",
"nette/utils": "^3.0",
"nette/finder": "^2.5",
"janbarasek/doctrine-pro": "~1.0",
"ramsey/uuid": "^3.0"
},
"autoload": {
"classmap": [
"src/"
]
},
"minimum-stability": "stable"
}
56 changes: 56 additions & 0 deletions config.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
parameters:
database:
primary:
host: 127.0.0.1
dbname: sandbox
user: root
password: root
driver: mysqli
journal:
enable: true
deleteOlderThan: 3 years
keepMaxRecords: 1500000

extensions:
console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
dbal: Baraja\Doctrine\DBAL\DI\DbalExtension
dbal.console: Baraja\Doctrine\DBAL\DI\DbalConsoleExtension(%consoleMode%)
orm: Baraja\Doctrine\ORM\DI\OrmExtension
orm.console: Baraja\Doctrine\ORM\DI\OrmConsoleExtension
orm.annotations: Baraja\Doctrine\ORM\DI\OrmAnnotationsExtension
baraja.database: Baraja\Doctrine\DatabaseExtension

dbal:
debug: %debugMode%
connection:
host: %database.primary.host%
driver: %database.primary.driver%
dbname: %database.primary.dbname%
user: %database.primary.user%
password: %database.primary.password%

orm:
entityManagerClass: Baraja\Doctrine\EntityManager

baraja.database:
types:
uuid: Baraja\Doctrine\UUID\UuidType

orm.annotations:
paths:
App: %appDir%
Model: %appDir%
Baraja: %rootDir%/vendor/baraja
ignore: []

console:
name: Baraja sandbox
version: '3.0'
catchExceptions: true
autoExit: true
url: http://baraja.cz
lazy: false

services:
- Baraja\Doctrine\DoctrineHelper
- Baraja\Doctrine\DoctrineHelperAccessor
20 changes: 20 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parameters:
ignoreErrors:
- '#Constant TEMP_DIR not found#'
- '#Constant TEST_DIR not found#'
- '#Parameter \#1 \$firstResult of method [^\s]+::setFirstResult\(\) expects int, (int\|)?null given#'
- '#Method Doctrine\\DBAL\\Driver\\Connection::query\(\) invoked with \d+ parameter, 0 required#'
- '#Call to an undefined method Serializable::__sleep\(\).#'
- '#Call to an undefined method Serializable::__wakeup\(\).#'
- '#Access to an undefined property Nette\\Http\\SessionSection::\$entity#'
- '#Call to an undefined method object::getId\(\)#' # class SimpleParameterFormatter

# console
- '#Constant TEMP_DIR not found#'
- '#Class Nette\\Framework not found#'
# Nette events are not annotated
- '#Call to an undefined method Nette\\Application\\Application::onShutdown\(\)#'
# it does, it just isn't annotated
- '#Static property Tracy\\Dumper::\$terminalColors \(mixed\[\]\) does not accept false#'
# intentional
- '#Constructor of class KdybyTests\\Console\\.*?Command.*? has an unused parameter *.?#'
82 changes: 82 additions & 0 deletions src/DatabaseExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Baraja\Doctrine;


use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\SQLite3Cache;
use Nette\DI\CompilerExtension;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Helpers;

class DatabaseExtension extends CompilerExtension
{

/**
* @var string[]
*/
private $types = [];

/**
* @param ClassType $class
*/
public function afterCompile(ClassType $class): void
{
$this->types = $this->getConfig();
$initialize = $class->getMethod('initialize');

$initialize->setBody(
$this->getTypeDefinition() . "\n"
. '/** @var ' . EntityManager::class . ' $entityManager */' . "\n"
. '$entityManager = $this->getByType(' . EntityManager::class . '::class);' . "\n"
. '$entityManager->setCache(' . $this->processCache() . ');' . "\n"
. '$entityManager->getConnection()->getSchemaManager()->getDatabasePlatform()'
. '->registerDoctrineTypeMapping(\'enum\', \'string\');' . "\n"
. '$entityManager->getConfiguration()->addCustomNumericFunction(\'rand\', ' . Rand::class . '::class);' . "\n"
. '$entityManager->buildCache();' . "\n"
. $initialize->getBody()
);
}

/**
* @return string
*/
private function getTypeDefinition(): string
{
$return = '';

foreach ($this->getConfig()['types'] ?? [] as $name => $className) {
$return .= '\Doctrine\DBAL\Types\Type::addType('
. Helpers::dump($name) . ',' . Helpers::dump($className)
. ');' . "\n";
}

return $return;
}

/**
* @return string
*/
private function processCache(): string
{
if (Utils::functionIsAvailable('apcu_cache_info')) {
$cache = new ApcuCache;
$cache->deleteAll();

if (Utils::functionIsAvailable('apcu_clear_cache')) {
@apcu_clear_cache();
}

return 'new ' . ApcuCache::class;
}

if (extension_loaded('sqlite3')) {
return 'new ' . SQLite3Cache::class . '(new \SQLite3($entityManager->getDbDirPath() . \'/doctrine.db\'), \'doctrine\')';
}

return 'null /* CACHE DOES NOT EXIST! */';
}

}
Loading

0 comments on commit 2156368

Please sign in to comment.