Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellvanw committed Mar 25, 2014
0 parents commit ba4320f
Show file tree
Hide file tree
Showing 19 changed files with 589 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev

script: phpunit
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 - Mitchell van Wijngaarden <[email protected]>

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.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Doctrine for Laravel

Doctrine implementation for Laravel 4

## Installation

Begin by installing the package through Composer. Edit your project's `composer.json` file to require `mitch/laravel-doctrine`.

```php
"require": {
"mitch/laravel-doctrine": "0.1.x"
}
```

Next use Composer to update your project from the the Terminal:

```php
php composer.phar update
```

Once the package has been installed you'll need to add the service provider. Open your `app/config/app.php` configuration file, and add a new item to the `providers` array.

```php
'Mitch\LaravelDoctrine\LaravelDoctrineServiceProvider'
```

After This you'll need to add the facade. Open your `app/config/app.php` configuration file, and add a new item to the `aliases` array.

```php
'EntityManager' => 'Mitch\LaravelDoctrine\EntityManagerFacade'
```

It's recommended to publish the package configuration.

```php
php artisan config:publish mitch/laravel-doctrine
```

## How It Works

This package gives you possibility to access the entity manager through the `EntityManager` facade.

```php
$product = new Product;
$product->setName('thinkpad');
$product->setPrice(1200);

EntityManager::persist($product);
EntityManager::flush();
```

For the full documentation on Doctrine [check out their docs](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/index.html)

## Commands

This package provides three artisan commands for your schema:

* doctrine:schema:create - Create database schema from models
* doctrine:schema:update - Update database schema to match models
* doctrine:schema:drop - Drop database schema
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "mitch/laravel-doctrine",
"description": "Doctrine implementation for Laravel 4",
"keywords": ["doctrine", "laravel"],
"license": "MIT",
"authors": [
{
"name": "Mitchell van Wijngaarden",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.1.*",
"doctrine/orm": "2.5.*",
"doctrine/migrations": "1.*"
},
"autoload": {
"psr-4": {
"Mitch\\LaravelDoctrine\\": "src/"
}
},
"minimum-stability": "stable"
}
46 changes: 46 additions & 0 deletions config/doctrine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

return [
// Available: APC, Xcache, Redis, Memcache
'provider' => null,

'connection' => [
'driver' => 'pdo_mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => ''
],

'metadata' => [
base_path('app/models')
],

'proxy' => [
'auto_generate' => false,
'directory' => null,
'namespace' => null
],

'cache' => [
'redis' => [
'host' => '127.0.0.1',
'port' => 6379,
'database' => 1
],
'memcache' => [
'host' => '127.0.0.1',
'port' => 11211
]
],

'migrations' => [
'directory' => 'database/doctrine-migrations',
'table' => 'doctrine_migrations'
],

'repository' => 'Doctrine\ORM\EntityRepository',

'logger' => null
];
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
35 changes: 35 additions & 0 deletions src/CacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php namespace Mitch\LaravelDoctrine;

class CacheManager
{
private $provider;
private $config;

public function __construct($provider, $config)
{
$this->provider = $provider;
$this->config = $config;
}

public function getCache()
{
return $this->provider ? $this->getCacheProvider($this->provider)->provide($this->getCacheConfig($this->provider)) : null;
}

private function getCacheProvider($provider)
{
$provider = ucfirst($provider);
$class = $this->getFullClassName("{$provider}Provider");
return new $class;
}

private function getFullClassName($class)
{
return "Mitch\\LaravelDoctrine\\CacheProviders\\{$class}";
}

private function getCacheConfig($provider)
{
return $this->config[$provider];
}
}
11 changes: 11 additions & 0 deletions src/CacheProviders/ApcProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace Mitch\LaravelDoctrine\CacheProviders;

use Doctrine\Common\Cache\ApcCache;

class ApcProvider implements Provider
{
public function provide($config = null)
{
return new ApcCache;
}
}
17 changes: 17 additions & 0 deletions src/CacheProviders/MemcacheProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace Mitch\LaravelDoctrine\CacheProviders;

use Doctrine\Common\Cache\MemcacheCache;
use Memcache;

class MemcacheProvider implements Provider
{
public function provide($config = null)
{
$memcache = new Memcache;
$memcache->connect($config['host'], $config['port']);

$cache = new MemcacheCache;
$cache->setMemcache($memcache);
return $cache;
}
}
6 changes: 6 additions & 0 deletions src/CacheProviders/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php namespace Mitch\LaravelDoctrine\CacheProviders;

interface Provider
{
public function provide($config = null);
}
21 changes: 21 additions & 0 deletions src/CacheProviders/RedisProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php namespace Mitch\LaravelDoctrine\CacheProviders;

use Doctrine\Common\Cache\RedisCache;
use Redis;

class RedisProvider implements Provider
{
public function provide($config = null)
{
$redis = new Redis();
$redis->connect($config['host'], $config['port']);

if (isset($config['database'])) {
$redis->select($config['database']);
}

$cache = new RedisCache();
$cache->setRedis($redis);
return $cache;
}
}
11 changes: 11 additions & 0 deletions src/CacheProviders/XcacheProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace Mitch\LaravelDoctrine\CacheProviders;

use Doctrine\Common\Cache\XcacheCache;

class XcacheProvider implements Provider
{
public function provider($config = null)
{
return new XcacheCache;
}
}
70 changes: 70 additions & 0 deletions src/Console/SchemaCreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php namespace Mitch\LaravelDoctrine\Console;

use Illuminate\Console\Command;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Symfony\Component\Console\Input\InputOption;

class SchemaCreateCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'doctrine:schema:create';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create database schema from models';

/**
* The schema tool.
*
* @var \Doctrine\ORM\Tools\SchemaTool
*/
private $tool;

/**
* The class metadata factory
*
* @var \Doctrine\ORM\Tools\SchemaTool
*/
private $metadata;

public function __construct(SchemaTool $tool, ClassMetadataFactory $metadata)
{
parent::__construct();

$this->tool = $tool;
$this->metadata = $metadata;
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
if ($this->option('sql')) {
$this->info('Outputting create query:');
$sql = $this->tool->getCreateSchemaSql($this->metadata->getAllMetadata());
$this->info(implode(';' . PHP_EOL, $sql));
} else {
$this->info('Creating database schema...');
$this->tool->createSchema($this->metadata->getAllMetadata());
$this->info('Schema has been created!');
}
}

protected function getOptions()
{
return [
['sql', null, InputOption::VALUE_OPTIONAL, 'Dumps SQL query and does not execute creation.']
];
}
}
Loading

0 comments on commit ba4320f

Please sign in to comment.