Skip to content

Commit

Permalink
[DoctrineBehaviors] init
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 15, 2016
0 parents commit d4e5d67
Show file tree
Hide file tree
Showing 38 changed files with 1,722 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
@@ -0,0 +1,7 @@
/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
phpunit.xml export-ignore
README.md export-ignore
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/vendor
composer.lock
6 changes: 6 additions & 0 deletions .scrutinizer.yml
@@ -0,0 +1,6 @@
tools:
external_code_coverage: true

checks:
php:
code_rating: true
34 changes: 34 additions & 0 deletions .travis.yml
@@ -0,0 +1,34 @@
language: php

sudo: false

php:
- 7.1

matrix:
include:
- php: 7.0
env: PHPUNIT_FLAGS="--coverage-clover coverage.xml" CHECK_CS=true

install:
# install composer dependencies
- composer install --prefer-source

script:
# run tests
- vendor/bin/phpunit $PHPUNIT_FLAGS
# check coding standard (defined in composer.json "scripts" section)
- if [[ "$CHECK_CS" != "" ]]; then composer check-cs; fi

after_script:
# upload coverage.xml file to Scrutinizer to analyze it
- |
if [[ "$PHPUNIT_FLAGS" != "" ]]; then
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.xml
fi
# do not send success notifications, they have no value
notifications:
email:
on_success: never
25 changes: 25 additions & 0 deletions LICENSE
@@ -0,0 +1,25 @@
The MIT License
---------------

Copyright (c) 2012 Tomáš Votruba (http://tomasvotruba.cz)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
122 changes: 122 additions & 0 deletions README.md
@@ -0,0 +1,122 @@
# Doctrine Behaviors

[![Build Status](https://img.shields.io/travis/Zenify/DoctrineBehaviors.svg?style=flat-square)](https://travis-ci.org/Zenify/DoctrineBehaviors)
[![Quality Score](https://img.shields.io/scrutinizer/g/Zenify/DoctrineBehaviors.svg?style=flat-square)](https://scrutinizer-ci.com/g/Zenify/DoctrineBehaviors)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/Zenify/DoctrineBehaviors.svg?style=flat-square)](https://scrutinizer-ci.com/g/Zenify/DoctrineBehaviors)
[![Downloads total](https://img.shields.io/packagist/dt/zenify/doctrine-behaviors.svg?style=flat-square)](https://packagist.org/packages/zenify/doctrine-behaviors)
[![Latest stable](https://img.shields.io/packagist/v/zenify/doctrine-behaviors.svg?style=flat-square)](https://packagist.org/packages/zenify/doctrine-behaviors)


Port of [KnpLabs/DoctrineBehaviors](https://github.com/KnpLabs/DoctrineBehaviors) to Nette DI

Supported behaviors:

- Blameable
- Geocodable
- Loggable
- Sluggable
- SoftDeletable
- Translatable
- Timestampable
- Tree

For implementation to entities, check [tests](https://github.com/KnpLabs/DoctrineBehaviors/tree/master/tests/fixtures/BehaviorFixtures/ORM).


## Install

Via Composer:

```sh
$ composer require zenify/doctrine-behaviors
```

Register extensions you need in `config.neon`:

```yaml
extensions:
translatable: Zenify\DoctrineBehaviors\DI\TranslatableExtension
- Zenify\DoctrineBehaviors\DI\TimestampableExtension
```


## Usage

### Translatable

Setup your translator locale callback in `config.neon`:

```yaml
translatable:
currentLocaleCallable: [@Translator, getLocale]
```

Place trait to your entity:

```php
class Article
{

use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
// returns translated property for $article->getTitle() or $article->title
use Zenify\DoctrineBehaviors\Entities\Attributes\Translatable;

}
```

And its translation entity:

```php
class ArticleTranslation
{

use Knp\DoctrineBehaviors\Model\Translatable\Translation;

/**
* @ORM\Column(type="string")
* @var string
*/
public $title;

}
```

For deeper knowledge see test for:

- [TranslatableEntity](https://github.com/KnpLabs/DoctrineBehaviors/blob/master/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntity.php)
- [TranslatableEntityTranslation](https://github.com/KnpLabs/DoctrineBehaviors/blob/master/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntityTranslation.php)
- [theirs use](https://github.com/KnpLabs/DoctrineBehaviors/blob/master/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php)


### Timestampable

Place trait to your entity to ad `$createdAt` and `$updatedAt` properties:

```php
class Article
{

use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable;

}
```



## Testing

```sh
composer check-cs
vendor/bin/phpunit
```


## Contributing

Rules are simple:

- new feature needs tests
- all tests must pass
- 1 feature per PR

We would be happy to merge your feature then!
37 changes: 37 additions & 0 deletions composer.json
@@ -0,0 +1,37 @@
{
"name": "zenify/doctrine-behaviors",
"description": "Integration of KnpLabs/DoctrineBehaviors to Nette\\DI",
"license": "MIT",
"authors": [
{ "name": "Tomáš Votruba", "homepage": "http://tomasvotruba.cz" }
],
"require": {
"php": "^7.0",
"kdyby/events": "~3.0",
"knplabs/doctrine-behaviors": "~1.4",
"tracy/tracy": "~2.4"
},
"require-dev": {
"kdyby/doctrine": "~3.0",
"nette/bootstrap": "~2.4",
"nette/di": "~2.4",
"nette/http": "~2.4",
"nette/reflection": "~2.4",
"nette/security": "~2.4",
"zenify/coding-standard": "^4.0",
"phpunit/phpunit": "^5.6"
},
"autoload": {
"psr-4": {
"Zenify\\DoctrineBehaviors\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Zenify\\DoctrineBehaviors\\Tests\\": "tests"
}
},
"scripts": {
"check-cs": "vendor/bin/phpcs src tests -ps --standard=vendor/zenify/coding-standard/src/ZenifyCodingStandard/ruleset.xml"
}
}
15 changes: 15 additions & 0 deletions phpunit.xml
@@ -0,0 +1,15 @@
<phpunit
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite>
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
41 changes: 41 additions & 0 deletions src/Blameable/UserCallable.php
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of Zenify
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
*/

namespace Zenify\DoctrineBehaviors\Blameable;

use Nette\Security\User;


final class UserCallable
{

/**
* @var User
*/
private $user;


public function __construct(User $user)
{
$this->user = $user;
}


/**
* @return mixed
*/
public function __invoke()
{
if ($this->user->isLoggedIn()) {
return $this->user->getId();
}
return NULL;
}

}
21 changes: 21 additions & 0 deletions src/Contract/Loggable/LoggerInterface.php
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* This file is part of Zenify
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
*/

namespace Zenify\DoctrineBehaviors\Contract\Loggable;


interface LoggerInterface
{

/**
* @param string $message
*/
function process($message);

}
59 changes: 59 additions & 0 deletions src/DI/AbstractBehaviorExtension.php
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* This file is part of Zenify
* Copyright (c) 2012 Tomas Votruba (http://tomasvotruba.cz)
*/

namespace Zenify\DoctrineBehaviors\DI;

use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\ServiceDefinition;
use Nette\DI\Statement;


abstract class AbstractBehaviorExtension extends CompilerExtension
{

protected function getClassAnalyzer() : ServiceDefinition
{
$builder = $this->getContainerBuilder();

if ($builder->hasDefinition('knp.classAnalyzer')) {
return $builder->getDefinition('knp.classAnalyzer');
}

return $builder->addDefinition('knp.classAnalyzer')
->setClass(ClassAnalyzer::class);
}


/**
* @return ServiceDefinition|NULL
*/
protected function buildDefinitionFromCallable(string $callable = NULL)
{
if ($callable === NULL) {
return NULL;
}

$builder = $this->getContainerBuilder();
$definition = $builder->addDefinition($this->prefix(md5($callable)));

list($definition->factory) = Compiler::filterArguments([
is_string($callable) ? new Statement($callable) : $callable
]);

list($resolverClass) = (array) $builder->normalizeEntity($definition->getFactory()->getEntity());
if (class_exists($resolverClass)) {
$definition->setClass($resolverClass);
}

return $definition;
}

}

0 comments on commit d4e5d67

Please sign in to comment.