Skip to content

Commit

Permalink
=refactoring, tests improved, psr2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
dubpub committed May 21, 2015
1 parent ae6231f commit 5c9289c
Show file tree
Hide file tree
Showing 33 changed files with 1,599 additions and 1,816 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/doc
/build
/.idea
/tests1
composer.phar
composer.lock
.DS_Store
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ ___laravel-commode/viewmodel___ package installed. Example:

Ajax separation calls are disabled by default, but you can enable it by overriding
``protected $separateRequests`` and setting it to ``protected $separateRequests = true;``. To define an ajax
method simply adding 'ajax_' prefix to your method name. Example:
method simply adding 'ajax' prefix to your method name. Example:

<?php namespace MyApp\Domain\Areas\Site\Controllers;

Expand All @@ -401,19 +401,19 @@ method simply adding 'ajax_' prefix to your method name. Example:
/**
* Returns JSONable results when ajax is triggered
**/
public function ajax_getLatest($lastId = null)
public function ajaxgetLatest($lastId = null)
{
return $this->postService->getLatest($lastId);
}

/**
* Returns same collection as ::ajax_getLatest() method,
* Returns same collection as ::ajaxgetLatest() method,
* but wraps it with view
**/
public function getLatest()
{
return \View::make('Site::posts.list', [
'posts' => $this->ajax_getLatest()
'posts' => $this->ajaxgetLatest()
]);
}
}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"license": "MIT",
"require": {
"php": ">=5.4.0",
"illuminate/support": "4.2.*"
"illuminate/support": "4.2.*",
"illuminate/routing": "4.2.*"
},
"require-dev": {
"mockery/mockery": "dev-master",
Expand Down
122 changes: 61 additions & 61 deletions src/LaravelCommode/Common/CommodeCommonServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
<?php namespace LaravelCommode\Common;
<?php

use Illuminate\Foundation\AliasLoader;
use LaravelCommode\Common\Constants\ServiceShortCuts;
use LaravelCommode\Common\GhostService\GhostService;
use LaravelCommode\Common\GhostService\GhostServices;
use LaravelCommode\Common\Resolver\Resolver;
use Illuminate\Support\ServiceProvider;
namespace LaravelCommode\Common;

use Illuminate\Foundation\AliasLoader;
use LaravelCommode\Common\Constants\ServiceShortCuts;
use LaravelCommode\Common\GhostService\GhostService;
use LaravelCommode\Common\GhostService\GhostServices;
use LaravelCommode\Common\Resolver\Resolver;

/**
* Class CommodeCommonServiceProvider
*
* Is a common service for all laravel-commode packages.
* It binds Resolver and GhostService manager.
*
* @author Volynov Andrew
* @package LaravelCommode\Common
*/
class CommodeCommonServiceProvider extends GhostService
{
protected $aliases = [
'CommodeResolver' => 'LaravelCommode\Common\Facades\Resolver'
];
/**
* Class CommodeCommonServiceProvider
* Get the services provided by the provider.
*
* Is a common service for all laravel-commode packages.
* It binds Resolver and GhostService manager.
* @return array
*/
public function provides()
{
return [ServiceShortCuts::GHOST_SERVICE, ServiceShortCuts::RESOLVER_SERVICE];
}

/**
* Bootstrap the application events.
*
* @author Volynov Andrew
* @package LaravelCommode\Common
* @return void
*/
class CommodeCommonServiceProvider extends GhostService
public function boot()
{
protected $aliases = [
'CommodeResolver' => 'LaravelCommode\Common\Facades\Resolver'
];
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [ServiceShortCuts::GHOST_SERVICE, ServiceShortCuts::RESOLVER_SERVICE];
}
$this->package('laravel-commode/common', 'commode-common', __DIR__.'/../../');
}

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('laravel-commode/common', 'commode-common', __DIR__.'/../../');
}
/**
* Will be triggered when the app's 'booting' event is triggered
*/
protected function launching()
{

/**
* Will be triggered when the app's 'booting' event is triggered
*/
public function launching() { }
}

/**
* Triggered when service is being registered
*/
public function registering()
{
$this->app->bindShared(ServiceShortCuts::RESOLVER_SERVICE, function()
{
return new Resolver($this->app);
});
/**
* Triggered when service is being registered
*/
protected function registering()
{
$this->app->bindShared(ServiceShortCuts::RESOLVER_SERVICE, function () {
return new Resolver($this->app);
});

$this->app->bindShared(ServiceShortCuts::GHOST_SERVICE, function()
{
return new GhostServices($this->app);
});
$this->app->bindShared(ServiceShortCuts::GHOST_SERVICE, function () {
return new GhostServices($this->app);
});

$this->app->bind(ServiceShortCuts::CORE_INITIALIZED, true);
$this->app->bind(ServiceShortCuts::CORE_INITIALIZED, true);

$this->app->booting(function ()
{
$loader = AliasLoader::getInstance();
$this->app->booting(function () {
$loader = AliasLoader::getInstance();

foreach($this->aliases as $facade => $facadeClass)
{
$loader->alias($facade, $facadeClass);
}
});
}
foreach ($this->aliases as $facade => $facadeClass) {
$loader->alias($facade, $facadeClass);
}
});
}
}
30 changes: 15 additions & 15 deletions src/LaravelCommode/Common/Constants/ServiceShortCuts.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php
namespace LaravelCommode\Common\Constants;
namespace LaravelCommode\Common\Constants;

/**
* Class ServiceShortCuts
*
* Constant for IoC bindings. Very handy for further refactoring.
*
* @author Volynov Andrey
* @package LaravelCommode\Common\Constants
*/
class ServiceShortCuts
{
const GHOST_SERVICE = 'commode.common.ghostservice';
const RESOLVER_SERVICE = 'commode.common.resolver';
const CORE_INITIALIZED = 'commode.common.loaded';
}
/**
* Class ServiceShortCuts
*
* Constant for IoC bindings. Very handy for further refactoring.
*
* @author Volynov Andrey
* @package LaravelCommode\Common\Constants
*/
class ServiceShortCuts
{
const GHOST_SERVICE = 'commode.common.ghostservice';
const RESOLVER_SERVICE = 'commode.common.resolver';
const CORE_INITIALIZED = 'commode.common.loaded';
}
146 changes: 70 additions & 76 deletions src/LaravelCommode/Common/Controllers/CommodeController.php
Original file line number Diff line number Diff line change
@@ -1,90 +1,84 @@
<?php
namespace LaravelCommode\Common\Controllers;
namespace LaravelCommode\Common\Controllers;

use Illuminate\Routing\Controller;
use LaravelCommode\Common\Constants\ServiceShortCuts;
use LaravelCommode\Common\Resolver\Resolver;
use Illuminate\Routing\Controller;
use LaravelCommode\Common\Constants\ServiceShortCuts;

/**
* Class CommodeController
*
* CommodeController is a laravel controller wrapper with some extended possibilities.
* It can resolve methods if protected $resolveMethods = true (true is the default value)
* and separate ajax calls like calling ajaxgetIndex() if method
* getIndex() exists, request is ajax and protected $separateRequests = true (false is the default value)
*
* @author Volynov Andrey
* @package LaravelCommode\Common\Controllers
*/
class CommodeController extends Controller
{
/**
* Class CommodeController
*
* CommodeController is a laravel controller wrapper with some extended possibilities.
* It can resolve methods if protected $resolveMethods = true (true is the default value)
* and separate ajax calls like calling ajax_getIndex() if method
* getIndex() exists, request is ajax and protected $separateRequests = true (false is the default value)
*
* @author Volynov Andrey
* @package LaravelCommode\Common\Controllers
* Determines if called methods need to be resolved.
* @var bool
*/
class CommodeController extends Controller
{
/**
* Determines if called methods need to be resolved.
* @var bool
*/
protected $resolveMethods = true;
protected $resolveMethods = true;

/**
* Determines if all ajax methods are prefixed with 'ajax_'
* @var bool
*/
protected $separateRequests = false;

/**
* Determines resolving is enabled. If it's enabled then
* it returns method resolved parameters, otherwise it
* returns parameters as they are.
*
* @param string $method Method name.
* @param array $params Array of known parameters.
* @return mixed
*/
private function checkParametersResolving($method, $params = array())
{
if (!$this->resolveMethods)
{
return $params;
}
/**
* Determines if all ajax methods are prefixed with 'ajax'
* @var bool
*/
protected $separateRequests = false;

$resolver = app(ServiceShortCuts::RESOLVER_SERVICE);
return $resolver->resolveMethodParameters($this, $method, $params);
/**
* Determines resolving is enabled. If it's enabled then
* it returns method resolved parameters, otherwise it
* returns parameters as they are.
*
* @param string $method Method name.
* @param array $params Array of known parameters.
* @return mixed
*/
private function checkParametersResolving($method, array $params = [])
{
if (!$this->resolveMethods) {
return $params;
}

/**
* Determines ajax separation is enabled. If it's enabled then
* it returns method's name prefixed with 'ajax', otherwise it
* returns method's name as it is.
*
* @param string $method Method name.
* @param bool|null $isAjax Determines if request is ajax.
* @return string
*/
private function checkAjaxMethod($method, $isAjax = null)
{
$isAjax = is_null($isAjax) ? app('request')->ajax() : $isAjax;

if ($this->separateRequests && $isAjax)
{
return 'ajax_'.$method;
}
return app()->make(ServiceShortCuts::RESOLVER_SERVICE)->resolveMethodParameters($this, $method, $params);
}

return $method;
/**
* Determines ajax separation is enabled. If it's enabled then
* it returns method's name prefixed with 'ajax', otherwise it
* returns method's name as it is.
*
* @param string $method Method name.
* @param bool|null $isAjax Determines if request is ajax.
* @return string
*/
private function checkAjaxMethod($method, $isAjax = null)
{
if ($this->separateRequests && $isAjax) {
return 'ajax'.$method;
}

/**
* Calls controller action.
*
* @param string $method
* @param array $params
* @return mixed
*/
public function callAction($method, $params = array())
{
$isAjax = app('request')->ajax();
return $method;
}

/**
* Calls controller action.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function callAction($method, array $parameters = [])
{
$isAjax = app()->make('request')->ajax();

$method = $this->checkAjaxMethod($method, $isAjax);
$params = $this->checkParametersResolving($method, $params);
$method = $this->checkAjaxMethod($method, $isAjax);
$parameters = $this->checkParametersResolving($method, $parameters);

return parent::callAction($method, $params);
}
}
return parent::callAction($method, $parameters);
}
}

0 comments on commit 5c9289c

Please sign in to comment.