-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New 3.0.x; removing HHVM support; minimum PHP 7.1
- Loading branch information
1 parent
7239da0
commit 22c2fc8
Showing
73 changed files
with
483 additions
and
483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
/nbproject/private/ | ||
/vendor/ | ||
/build/ | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
sudo: required | ||
dist: trusty | ||
services: | ||
- mongodb | ||
language: php | ||
php: | ||
- 7 | ||
- hhvm | ||
matrix: | ||
allow_failures: | ||
- php: hhvm | ||
- 7.1 | ||
- 7.2 | ||
before_install: | ||
- composer self-update | ||
install: | ||
- composer update --ignore-platform-reqs --prefer-source | ||
before_script: | ||
- if [[ $TRAVIS_PHP_VERSION != "hhvm" ]]; then phpenv config-add travis-php.ini; fi | ||
- if [[ $TRAVIS_PHP_VERSION = "hhvm" ]]; then sudo cat travis-php.ini >> /etc/hhvm/php.ini; ./build-mongodb.sh; fi | ||
- phpenv config-add travis-php.ini | ||
script: | ||
- vendor/bin/phpunit --coverage-clover=coverage.clover | ||
after_script: | ||
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi;' | ||
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi;' | ||
- wget https://scrutinizer-ci.com/ocular.phar | ||
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover | ||
cache: | ||
directories: | ||
- $HOME/.composer/cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Exception Hierarchy | ||
|
||
We provide a mechanism to translate vendor-specific exceptions into a standard exception hierarchy. | ||
|
||
## Reason | ||
|
||
It's all about separation of concerns and abstraction. Using the translation utilities in this library, you can wrap vendor-specific exceptions in these generic exceptions. | ||
|
||
It decouples your persistence layer from the rest of your application. You can swap out the library you use to store data and not have to worry about changes in the rest of your application. | ||
|
||
## Exception Classes | ||
|
||
All of the following classes are in the `Caridea\Dao\Exception` namespace: | ||
|
||
* `Inoperable` – An exception for invalid API usage and configuration problems (extends from `\LogicException`) | ||
* `Transient` – An abstract super class for problems that might not occur a second time (extends from `\RuntimeException`) | ||
* `Conflicting` – An exception for concurrency failures | ||
* `Unreachable` – An exception for connection problems | ||
* `Permanent` – An abstract super class for problems that will probably occur a second time (extends from `\RuntimeException`) | ||
* `Generic` – An exception for any other problem not covered elsewhere | ||
* `Locked` – An exception for unwritable records | ||
* `Unretrievable` – An exception for unexpected results, for instance no results or too many results | ||
* `Violating` – An exception for constraint violations | ||
* `Duplicative` – An exception for unique constraint violations | ||
|
||
## Translators | ||
|
||
We distribute two translation utilities with this library: one for the popular [Doctrine ORM library](http://www.doctrine-project.org/), the other for the popular [MongoDB library](https://github.com/mongodb/mongo-php-library). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Abstract DAO Classes | ||
|
||
This library comes with two super-classes to be extended by your own projects: | ||
|
||
* `Caridea\Dao\Doctrine2` – designed for the popular [Doctrine ORM library](http://www.doctrine-project.org/) | ||
* `Caridea\Dao\MongoDb` – designed for the popular [MongoDB library](https://github.com/mongodb/mongo-php-library) | ||
|
||
They both extend from `Caridea\Dao\Dao`, which is an abstract class that implements `Psr\Log\LoggerAwareInterface`. | ||
|
||
## Doctrine | ||
|
||
The Doctrine DAO constructor takes the `Doctrine\ORM\EntityManager`, the name of the entity, and an optional `Psr\Log\LoggerInterface`. Once created, the following protected properties are available to subclasses: | ||
|
||
* `$manager` – The `EntityManager` you provided | ||
* `$entityName` – The `string` entity name you provided | ||
* `$repository` – A `Doctrine\ORM\EntityRepository` retrieved from the `EntityManager` | ||
* `$logger` – A `Psr\Log\LoggerInterface`, guaranteed to be non-null | ||
|
||
There are also two very useful protected methods available to subclasses. | ||
|
||
### The doExecute Method | ||
|
||
The `doExecute` method accepts a `Closure` that gets passed the `EntityManager`. | ||
|
||
If an exception occurs while your function is being executed, it's translated and wrapped by one of the exceptions included in this library. | ||
|
||
```php | ||
class MyDao extends \Caridea\Dao\Doctrine2 | ||
{ | ||
public function create($record) | ||
{ | ||
$this->logger->info("Creating the record"); | ||
$this->doExecute(function ($manager) use ($record) { | ||
$manager->persist($record); | ||
$manager->flush(); | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
### The doExecuteInRepository Method | ||
|
||
Similarly, the `doExecuteInRepository` method accepts a `Closure` that gets passed the `EntityRepository`. | ||
|
||
If an exception occurs while your function is being executed, it's translated and wrapped by one of the exceptions included in this library. | ||
|
||
```php | ||
class MyDao extends \Caridea\Dao\Doctrine2 | ||
{ | ||
public function find($id) | ||
{ | ||
return $this->doExecuteInRepository(function ($repository) { | ||
return $repository->find($id); | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
## MongoDB | ||
|
||
The MongoDB DAO constructor takes the `MongoDB\Driver\Manager`, the name of the collection, and an optional `Psr\Log\LoggerInterface`. Once created, the following protected properties are available to subclasses: | ||
|
||
* `$manager` – The `Manager` you provided | ||
* `$collection` – The `string` collection name you provided | ||
* `$logger` – A `Psr\Log\LoggerInterface`, guaranteed to be non-null | ||
|
||
There are also a very useful protected method available to subclasses. | ||
|
||
### The doExecute Method | ||
|
||
The `doExecute` method accepts a `Closure` that gets passed the `Manager` and collection name. | ||
|
||
If an exception occurs while your function is being executed, it's translated and wrapped by one of the exceptions included in this library. | ||
|
||
```php | ||
class MyDao extends \Caridea\Dao\MongoDb | ||
{ | ||
public function create($record) | ||
{ | ||
$this->logger->info("Creating the record"); | ||
$this->doExecute(function ($manager, $collection) use ($record) { | ||
$bulk = new \MongoDB\Driver\BulkWrite(); | ||
$bulk->insert($record); | ||
return $manager->executeBulkWrite($collection, $bulk); | ||
}); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Events | ||
|
||
This library includes several events that can be broadcast, and a trait you can use in your DAOs to broadcast these events. | ||
|
||
## Event Classes | ||
|
||
The following classes are all in the `Caridea\Dao\Event` namespace. They all extend from the `Caridea\Dao\Event` class, which includes a method: `getEntity` that returns the entity involved. | ||
|
||
* `PreDelete` – To be broadcast before a delete event | ||
* `PostDelete` – To be broadcast after a delete event | ||
* `PreInsert` – To be broadcast before an insert event | ||
* `PostInsert` – To be broadcast after an insert event | ||
* `PreUpdate` – To be broadcast before an update event | ||
* `PostUpdate` – To be broadcast after an update event | ||
|
||
## Publishing Trait | ||
|
||
The `Caridea\Dao\Event\Publishing` trait has methods your DAO can call that publish the above events. They all accept a single argument that is the entity involved. | ||
|
||
* `preDelete` – publishes a `PreDelete` event | ||
* `postDelete` – publishes a `PostDelete` event | ||
* `preInsert` – publishes a `PreInsert` event | ||
* `postInsert` – publishes a `PostInsert` event | ||
* `preUpdate` – publishes a `PreUpdate` event | ||
* `postUpdate` – publishes a `PostUpdate` event | ||
|
||
Here's an example class that uses this trait: | ||
|
||
```php | ||
class MyDao extends \Caridea\Dao\MongoDb implements \Caridea\Event\PublisherAware | ||
{ | ||
use \Caridea\Dao\Event\Publishing; | ||
|
||
public function create($record) | ||
{ | ||
$this->preInsert($record); | ||
$this->doExecute(function () { | ||
// do some persistence magic | ||
}); | ||
$this->postInsert($record); | ||
} | ||
} | ||
``` | ||
|
||
When creating an instance of this class, you'd want to make sure to call `setPublisher`, or better yet, use [caridea/container](https://github.com/libreworks/caridea-container) to factory your DAO. | ||
|
||
## But My Persistence Library Has Events… | ||
|
||
Awesome! Don't use this trait, then! If your library (e.g. Doctrine) already handles events that occur to entities, then this trait becomes irrelevant. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# caridea/dao | ||
|
||
🍤 Caridea is a miniscule PHP application library. This shrimpy fellow is what you'd use when you just want some helping hands and not a full-blown framework. | ||
|
||
This is its Data Access Object support library. You can use these classes to support DAOs that you write. | ||
|
||
* [Exception Hierarchy](01-exceptions.md) | ||
* [Abstract DAO Classes](02-daos.md) | ||
* [Events](03-events.md) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.