Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
Make all constructors public
Browse files Browse the repository at this point in the history
Now they can be used with an external dependency injection framework
  • Loading branch information
qurben committed Nov 30, 2019
1 parent 4dc8b40 commit 8a28b99
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# C.S.R. Delft ORM

A simple object-relational mapper for PHP. We currently use this library in production
on [csrdelft.nl](https://csrdelft.nl).
on [csrdelft.nl](https://csrdelft.nl).

## Installation

Expand Down Expand Up @@ -41,7 +41,7 @@ overview of the basic things you need to know in order to get started.

An entity is an object containing data, like for instance a car, person, etc.

When you want to save an entity to the database, you'll have to extend the class
When you want to save an entity to the database, you'll have to extend the class
`PersistentEntity`. An entity must contain a few variables, which are discussed
below. An entity must only contain logic about itself, not logic about other classes
or about other instances of the same entity. This should be in the Model (or the controller which is
Expand Down Expand Up @@ -76,7 +76,7 @@ A Type is an array, with the following values.

* 0: Type from `T` (`PersistentAttributeType.enum`)
* 1: Is this variable nullable?
* 2: If 0 is `T::Enumeration`, the enum class (extends `PersistentEnum`). Else 'extra',
* 2: If 0 is `T::Enumeration`, the enum class (extends `PersistentEnum`). Else 'extra',
for instance `auto_increment` or comment.

```php
Expand Down Expand Up @@ -140,8 +140,8 @@ class Car extends PersistentEntity {

### Model

A model has to extend the `PersistenceModel` class. A model is the owner of a
specific entity. A model can be accessed everywhere with the public static
A model has to extend the `PersistenceModel` class. A model is the owner of a
specific entity. A model can be accessed everywhere with the public static
`instance()` method. This should however be avoided where possible.

Models should be placed in `model/`.
Expand Down Expand Up @@ -176,9 +176,9 @@ The following functions can be used on a model

### `find($criteria, $criteria_params, ...) : PersistentEntity[]`

Find entities in the database filtered on criteria. The syntax for this should be familiar if you
Find entities in the database filtered on criteria. The syntax for this should be familiar if you
ever worked with PDO in PHP. The `$criteria` is the `WHERE` clause of the underlying select statement, you can
put `?`'s here where variables are. The criteria params are where you fill these variables. Criteria
put `?`'s here where variables are. The criteria params are where you fill these variables. Criteria
params are automatically filtered and safe for user input.

```php
Expand All @@ -192,7 +192,7 @@ Count the number of entities which pass the criteria, same as `find(..)`. Create

### `exists($entity) : boolean`

Check whether or not an entity exists in the database.
Check whether or not an entity exists in the database.

### `create($entity) : string`

Expand All @@ -215,7 +215,7 @@ require_once 'model/entity/Car.php';

class CarModel extends PersistenceModel {
const ORM = 'Car';

public function findByColor($color) {
return $this->find('color = ?', [$color]);
}
Expand Down Expand Up @@ -278,7 +278,7 @@ $car->reviews = [$review];
$model->update($car);
```

To prevent remote code execution only allowed classes can be (de)deserialized. In the third element of the attribute defintion
To prevent remote code execution only allowed classes can be (de)deserialized. In the third element of the attribute defintion
the list of allowed classes should be specified. Also null can be passed to allow all classes.
## Database transactions

Expand All @@ -298,30 +298,30 @@ Database::transaction(function () use ($car) {
## Dependency Injection

The orm does a very simple way of dependency injection. When a instance of a model is created is created
it tries to lookup any dependencies which extend `DependencyManager` if they are found they are wired into the
it tries to lookup any dependencies which extend `DependencyManager` if they are found they are wired into the
model and available for use. There can only be one version of a model and this is kept track of in `DependencyManager`.

### Example

```php
class OwnerModel extends PersistenceModel {
const ORM = 'Owner';

/** @var CarModel */
protected $carModel;
protected function __construct(CarModel $carModel) {

public function __construct(CarModel $carModel) {
$this->carModel = $carModel;
}

public function getCarsForOwner(Owner $owner) {
return $this->carModel->find('owner = ?', [$owner->id]);
}
}

class CarModel extends PersistenceModel {
const ORM = 'Car';

public function findByColor($color) {
return $this->find('color = ?', [$color]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/CachedPersistenceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class CachedPersistenceModel extends PersistenceModel {
/**
* PersistenceModel constructor.
*/
protected function __construct() {
public function __construct() {
parent::__construct();

$this->memcache = OrmMemcache::instance()->getCache();
Expand Down
2 changes: 1 addition & 1 deletion src/DynamicEntityModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function makeModel($table_name) {
*
* @param string $table_name
*/
protected function __construct($table_name) {
public function __construct($table_name) {
parent::__construct();
$this->definition = new DynamicEntityDefinition();
$this->definition->table_name = $table_name;
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/OrmMemcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OrmMemcache extends DependencyManager {
* OrmMemcache constructor.
* @param $path
*/
protected function __construct($path) {
public function __construct($path) {
if (class_exists('Memcache')) {
$this->cache = new Memcache();
if ($this->cache->connect('unix://' . $path . 'csrdelft-cache.socket', 0)) {
Expand Down
2 changes: 1 addition & 1 deletion src/PersistenceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function __static() {
/**
* PersistenceModel constructor.
*/
protected function __construct() {
public function __construct() {
assert(static::ORM !== null);
$orm = static::ORM;
$this->orm = new $orm();
Expand Down
16 changes: 8 additions & 8 deletions tests/DependencyManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
require_once 'Persistence/MySqlDatabaseTestCase.php';

class OneParameter extends DependencyManager {
protected function __construct($parameter) {
public function __construct($parameter) {
}
}

class CircularOne extends DependencyManager {
protected function __construct(CircularTwo $circularTwo) {
public function __construct(CircularTwo $circularTwo) {
}
}

class CircularTwo extends DependencyManager {
protected function __construct(CircularOne $circularOne) {
public function __construct(CircularOne $circularOne) {
}
}

class NoParameter extends DependencyManager {
protected function __construct() {
public function __construct() {
}
}

Expand All @@ -30,7 +30,7 @@ class NoConstructor extends DependencyManager {
}

class ParameterMismatch extends DependencyManager {
protected function __construct(EmptyTest $emptyTest) {
public function __construct(EmptyTest $emptyTest) {
}
}

Expand All @@ -40,7 +40,7 @@ class PreloadDependency extends DependencyManager {
*/
public $emptyTest;

protected function __construct(EmptyTest $emptyTest) {
public function __construct(EmptyTest $emptyTest) {
$this->emptyTest = $emptyTest;
}
}
Expand All @@ -51,7 +51,7 @@ class Normal extends DependencyManager {
public $noParameter;
public $data;

protected function __construct($data, NoParameter $noParameter) {
public function __construct($data, NoParameter $noParameter) {
$this->data = $data;
$this->noParameter = $noParameter;
}
Expand All @@ -61,7 +61,7 @@ class Reversed extends DependencyManager {
public $noConstructor;
public $data;

protected function __construct(NoConstructor $noConstructor, $data) {
public function __construct(NoConstructor $noConstructor, $data) {
$this->data = $data;
$this->noConstructor = $noConstructor;
}
Expand Down

0 comments on commit 8a28b99

Please sign in to comment.