Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Feb 16, 2018
0 parents commit 9850a12
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
28 changes: 28 additions & 0 deletions _config/proxydb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
Name: proxydb-mysql
After: '#databaseconnectors'
---
SilverStripe\Core\Injector\Injector:
MySQLDatabase:
factory: TractorCow\SilverStripeProxyDB\ProxyDBFactory
MySQLPDODatabase:
factory: TractorCow\SilverStripeProxyDB\ProxyDBFactory
---
Name: proxydb-postgresql
After: '#postgresqlconnectors'
---
SilverStripe\Core\Injector\Injector:
PostgrePDODatabase:
factory: TractorCow\SilverStripeProxyDB\ProxyDBFactory
PostgreSQLDatabase:
factory: TractorCow\SilverStripeProxyDB\ProxyDBFactory

---
Name: proxydb-sqlite
After: '#sqlite3connectors'
---
SilverStripe\Core\Injector\Injector:
SQLite3PDODatabase:
factory: TractorCow\SilverStripeProxyDB\ProxyDBFactory
SQLite3Database:
factory: TractorCow\SilverStripeProxyDB\ProxyDBFactory
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "tractorcow/silverstripe-proxy-db",
"type": "silverstripe-vendormodule",
"license": "BSD-3-Clause",
"keywords": [
"silverstripe",
"db",
"proxy"
],
"authors": [
{
"name": "Damian Mooyman",
"email": "[email protected]"
}
],
"require": {
"silverstripe/framework": "^4@dev",
"silverstripe/vendor-plugin": "^1.0",
"tractorcow/classproxy": "^0.1.1"
},
"autoload": {
"psr-4": {
"TractorCow\\SilverStripeProxyDB\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
38 changes: 38 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Database proxy

Ok, so you want to proxy the database.

Install this module, and decorate the factory with code you want to extend

```
---
Name: myproxydb
After: '#proxydb'
---
TractorCow\SilverStripeProxyDB\ProxyDBFactory:
extensions:
- ProxyDBExtension
```

Then in your code you can do this

```
<?php
use SilverStripe\Core\Extension;
use TractorCow\ClassProxy\Generators\ProxyGenerator;
class ProxyDBExtension extends Extension
{
public function updateProxy(ProxyGenerator &$proxy)
{
$proxy = $proxy->addMethod('manipulate', function ($args, $next) {
SearchManipulator::manipulate($args[0]);
return $next(...$args);
});
}
}
```

You can chain methods; All addMethod() calls on the same method name will
form a set of middleware. First methods registered are executed first.
28 changes: 28 additions & 0 deletions src/ProxyDBFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace TractorCow\SilverStripeProxyDB;

use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Factory;
use TractorCow\ClassProxy\ProxyFactory;

class ProxyDBFactory implements Factory
{
use Extensible;

/**
* Creates a new service instance.
*
* @param string $service The class name of the service.
* @param array $params The constructor parameters.
* @return object The created service instances.
*/
public function create($service, array $params = array())
{
$proxy = ProxyFactory::create($service);
$this->extend('updateProxy', $proxy);
$instance = $proxy->instance($params);
$this->extend('updateInstance', $instance);
return $instance;
}
}

0 comments on commit 9850a12

Please sign in to comment.