Last active
August 29, 2015 14:10
-
-
Save 0xMatt/09a0218ba5b3399363c8 to your computer and use it in GitHub Desktop.
Laravel 5 Repositories
This file contains hidden or 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
<?php | |
namespace Lithe\Database; | |
use Illuminate\Database\Eloquent\Model; | |
use Lithe\Contracts\Database\RepositoryInterface; | |
use Illuminate\Contracts\Container\Container; | |
abstract class Repository implements RepositoryInterface | |
{ | |
/** | |
* The eloquent object | |
* | |
* @var object | |
*/ | |
protected $model; | |
/** | |
* The models class | |
* | |
* @var string | |
*/ | |
protected $class; | |
/** | |
* | |
* @param Container $app | |
*/ | |
public function __construct(Container $app) | |
{ | |
$this->model = $app->make($this->class); | |
} | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \Lithe\Contracts\Database\RepositoryInterface::create() | |
*/ | |
public function create(array $attributes) | |
{ | |
return $this->model->create($attributes); | |
} | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \Lithe\Contracts\Database\RepositoryInterface::update() | |
*/ | |
public function update($clause, array $attributes) | |
{ | |
$info = (! is_array($clause) ? [ | |
'id' => $clause | |
] : (array) $clause); | |
return $this->model->update($info, $attributes); | |
} | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \Lithe\Contracts\Database\RepositoryInterface::all() | |
*/ | |
public function all($columns = array('*')) | |
{ | |
return $this->model->all($columns); | |
} | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \Lithe\Contracts\Database\RepositoryInterface::find() | |
*/ | |
public function find($id, $columns = array('*')) | |
{ | |
return $this->model->find($id, $columns); | |
} | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \Lithe\Contracts\Database\RepositoryInterface::paginate() | |
*/ | |
public function paginate($perPage = null, $columns = array('*')) | |
{ | |
return $this->model->paginate($perPage); | |
} | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \Lithe\Contracts\Database\RepositoryInterface::destroy() | |
*/ | |
public function destroy($ids) | |
{ | |
return $this->model->destroy($ids); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Lithe\Contracts\Database; | |
interface RepositoryInterface | |
{ | |
/** | |
* | |
* @param array $columns | |
*/ | |
public function all($columns = array('*')); | |
/** | |
* | |
* @param int $id | |
* @param array $columns | |
*/ | |
public function find($id, $columns = array('*')); | |
/** | |
* | |
* @param int $perPage | |
* @param array $columns | |
*/ | |
public function paginate($perPage = null, array $columns = array('*')); | |
/** | |
* | |
* @param array $attributes | |
*/ | |
public function create(array $attributes); | |
/** | |
* | |
* @param mixed $clause | |
* @param array $attributes | |
*/ | |
public function update($clause, array $attributes); | |
/** | |
* | |
* @param mixed $ids | |
*/ | |
public function destroy($ids); | |
} |
This file contains hidden or 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
<?php | |
namespace Modules\System\Http\Controllers\Backend\Users; | |
use Modules\System\Domain\Repositories\User\UserRepositoryInterface; | |
use Modules\System\Domain\Repositories\Group\GroupRepositoryInterface; | |
use Modules\System\Http\Requests\Backend\UserRequest; | |
class UserController extends \Lithe\Cms\Controller | |
{ | |
/** | |
* | |
* @var UserRepository | |
*/ | |
protected $userRepo; | |
/** | |
* | |
* @var GroupRepository | |
*/ | |
protected $groupRepo; | |
/** | |
* | |
* @param UserRepositoryInterface $userRepo | |
* @param GroupRepositoryInterface $groupRepo | |
*/ | |
public function __construct(UserRepositoryInterface $userRepo, GroupRepositoryInterface $groupRepo) | |
{ | |
parent::__construct(); | |
$this->userRepo = $userRepo; | |
$this->groupRepo = $groupRepo; | |
} | |
/** | |
* User listing | |
* | |
* @return object \Illuminate\View\View | |
*/ | |
public function index() | |
{ | |
return view('users/list', [ | |
'users' => $this->userRepo->paginate() | |
]); | |
} | |
/** | |
* | |
* @return \Illuminate\View\View | |
*/ | |
public function create() | |
{ | |
return view('users/form', [ | |
'groups' => $this->groupRepo->all() | |
]); | |
} | |
/** | |
* | |
* @param unknown $id | |
*/ | |
public function show($id) | |
{ | |
$user = $this->userRepo->find($id); | |
// Protect root | |
if ($user->is_root && ! $this->user->is_root) { | |
return redirect()->back()->withAlert([ | |
'You are not allowed to edit this user.' | |
]); | |
} | |
return view('users/form', [ | |
'user' => $user, | |
'groups' => $this->groupRepo->all() | |
]); | |
} | |
/** | |
* | |
* @param UserRequest $request | |
*/ | |
public function postCreate(UserRequest $request) | |
{ | |
$this->userRepo->create($request->only('username','email','password','group_id')); | |
// Fire events | |
// | |
return redirect()->back()->withAlert('User has been created successfully'); | |
} | |
/** | |
* Add/edit user post action | |
* | |
* @access public | |
*/ | |
public function postShow(UserRequest $request, $id) | |
{ | |
$this->repo->update($id, $request->only('username','email','group_id')); | |
// Fire events | |
// | |
return redirect()->back()->withAlert('User information updated with success'); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Modules\System\Domain\Repositories\User; | |
use Lithe\Database\Repository; | |
use Illuminate\Http\Request; | |
class UserRepository extends Repository implements UserRepositoryInterface | |
{ | |
/** | |
* The model class | |
* | |
* @var string | |
*/ | |
protected $class = 'Modules\System\Domain\Models\User'; | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \LaravelCms\Database\Repository::create() | |
*/ | |
public function create(array $attributes) | |
{ | |
// Create the user | |
$user = parent::create($attributes); | |
// Check request for current group, if none, get default | |
$user->group()->attach(($attributes['group'] ? $attributes['group'] : 2)); | |
// The event will assign confirmation code by reference | |
// and also send out validation email if necessary | |
// Event::fire('user.create', $user); | |
$user->save(); | |
} | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \Lithe\Database\Repository::update() | |
*/ | |
public function update($data, array $attributes) | |
{ | |
// Update user data | |
$user = parent::update($data, $attributes); | |
// Sync groups | |
$user->sync($attributes['groups']); | |
// Fire events | |
// Event::fire('user.update'); | |
} | |
/** | |
* (non-PHPdoc) | |
* | |
* @see \Lithe\Database\Repository::destroy() | |
*/ | |
public function destroy($id) | |
{ | |
// Get the user data first | |
$user = parent::find($id); | |
// Remove user content | |
$manager = new ContentManager($user); | |
$manager->purgeAllContent(); | |
// Remove group associations | |
$user->groups()->detach(); | |
// Remove user | |
$user->delete(); | |
// Fire Events | |
// Event::fire('user.delete'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment