Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/vendor
/vendor/
composer.lock
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,34 @@ Via Composer
$ composer require czim/laravel-repository
```

If you would like to use the repository generator, you will have to add the RepositoryServiceProvider to your config/app.php
``` php
Czim\Repository\RepositoryServiceProvider::class,
```

Publish the repostory configuration file.

``` bash
php artisan vendor:publish --tag="repository"
```

## Basic Usage

Simply extend the (abstract) repository class of your choice, either `Czim\Repository\BaseRepository`, `Czim\Repository\ExtendedRepository` or `Czim\Repository\ExtendedPostProcessingRepository`.

The only abstract method that must be provided is the `model` method (this is just like the way Bosnadev's repositories are used).

To use the following command, please add the service provider as stated in the **Install** section.

### Make Repository
This commands automaticly creates a new Eloquent model repository class. It will also attempt to link the correct Eloquent model, Make sure to confirm this.

``` bash
php artisan make:repository PostsRepository
```

The above command will create a repository class named PostsRepository and link the Post model to it.

### Base-, Extended- and PostProcessing

Depending on what you require, three different abstract repository classes may be extended:
Expand Down
24 changes: 24 additions & 0 deletions config/repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

return [
/**
* The namespace for repositories generated by the make:repository command.
*/
'namespace' => 'App\\Repositories',

/**
* The base repository class to extend new repositories with.
*/
'base' => \CZim\Repository\BaseRepository::class,

/**
* The suffix of each and every Repository.
* This will be subtracted to generate de propable model class names.
*/
'suffix' => 'Repository',

/**
* The locations where Eloquent models are stored.
*/
'models' => 'App',
];
203 changes: 203 additions & 0 deletions src/Console/Commands/RepositoryMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

namespace Czim\Repository\Console\Commands;

use Czim\Repository\BaseRepository;
use Czim\Repository\RepositoryServiceProvider;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Filesystem\Filesystem;

class RepositoryMakeCommand extends GeneratorCommand
{
/**
* The namespace for repositories generated by the make:repository command.
*
* @var string
*/
private $namespace = 'App\\Repositories';

/**
* The base repository class to look for.
*
* @var string
*/
private $base = BaseRepository::class;

/**
* The suffix of each and every Repository.
* This will be subtracted to generate de propable model class names.
*
* @var string
*/
private $suffix = 'Repository';

/**
* The locations where Eloquent models are stored.
*
* @var string
*/
private $models = 'App';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'make:repository';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new Eloquent model repository class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Repository';

/**
* Create a new command instance.
* @param Filesystem $fileSystem
*/
public function __construct(Filesystem $fileSystem)
{
$this->loadConfig();

parent::__construct($fileSystem);
}

/**
* Load the configuration for the command.
*/
private function loadConfig()
{
$this->namespace = config('repository.namespace', $this->namespace);
$this->base = config('repository.base', $this->base);
$this->suffix = config('repository.suffix', $this->suffix);
$this->models = config('repository.models', $this->models);
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return RepositoryServiceProvider::$packagePath . '/stubs/repository.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
public function getDefaultNamespace($rootNamespace)
{
return $this->namespace;
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$stub = parent::buildClass($name);

$modelName = $this->getModelClass($name);

$this->replaceModelNamespace($stub, $modelName);
$this->replaceModelClass($stub, $modelName);

$this->replaceBaseRepositoryNamespace($stub, $this->base);
$this->replaceBaseRepositoryClass($stub, $this->base);

return $stub;
}

/**
* Replace the probable namespace for the given stub.
*
* @param string $stub
* @param string $name
* @return $this
*/
private function replaceModelNamespace(&$stub, $name)
{
$stub = str_replace('DummyModelNamespace', $name, $stub);

return $this;
}

/**
* Replace the probable model class name for the given stub.
*
* @param string $stub
* @param string $name
* @return $this
*/
private function replaceModelClass(&$stub, $name)
{
$names = explode('\\', $name);
$class = array_pop($names);

$stub = str_replace('DummyModelClass', $class, $stub);

return $this;
}

/**
* Get the class name of the probable associated model.
*
* @param $name
* @return string
*/
private function getModelClass($name)
{
$repositoryClass = str_replace($this->getNamespace($name) . '\\', '', $name);
$class = str_replace($this->suffix, '', $repositoryClass);

$classSingular = str_singular($class);

$modelClass = "{$this->models}\\{$classSingular}";

return $modelClass;
}

/**
* Replace the default base repository class namespace for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
private function replaceBaseRepositoryNamespace(&$stub, $name)
{
$stub = str_replace('BaseRepositoryNamespace', $name, $stub);

return $this;
}

/**
* Replace the default base repository class name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
private function replaceBaseRepositoryClass(&$stub, $name)
{
$baseClass = str_replace($this->getNamespace($name) . '\\', '', $name);
$stub = str_replace('BaseRepositoryClass', $baseClass, $stub);

return $this;
}
}
36 changes: 36 additions & 0 deletions src/RepositoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,51 @@

namespace Czim\Repository;

use Czim\Repository\Console\Commands\RepositoryMakeCommand;
use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
/**
* Commands provided by the current package.
*
* @var array
*/
private $commands = [
RepositoryMakeCommand::class,
];

/**
* The base package path.
*
* @var string
*/
public static $packagePath = null;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
self::$packagePath = dirname(__DIR__);

$this->publishes(
[
self::$packagePath . '/config/repository.php' => config_path('repository.php'),
],
'repository'
);
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->commands($this->commands);
}
}
21 changes: 21 additions & 0 deletions stubs/repository.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace DummyNamespace;

use BaseRepositoryNamespace;
use DummyModelNamespace;

class DummyClass extends BaseRepositoryClass
{
/**
* Returns specified model class name.
*
* Note that this is the only method.
*
* @return string
*/
public function model()
{
return DummyModelClass::class;
}
}