Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto search controllers #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 52 additions & 24 deletions src/controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,71 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace Serverfireteam\Panel;

use \Serverfireteam\Panel\libs\PanelElements;
use Illuminate\Routing\Controller;
use Symfony\Component\Finder\Finder;

class MainController extends Controller {


public function entityUrl($entity, $methods){
class MainController extends Controller
{


$appHelper = new libs\AppHelper();
public function entityUrl($entity, $methods)
{
$appHelper = new libs\AppHelper();

if ( \Links::isMain($entity)){
if (\Links::isMain($entity)) {
$controller_path = 'Serverfireteam\Panel\\'.$entity.'Controller';
} else {
} else {
$panel_path = \Config::get('panel.controllers');
if ( isset($panel_path) ){
$controller_path = '\\'.$panel_path.'\\'.$entity.'Controller';
} else {
$controller_path = $appHelper->getNameSpace().'Http\Controllers\\'.$entity.'Controller';
}
}

try{
$controller = \App::make($controller_path);
}catch(\Exception $ex){
throw new \Exception("Controller not found ( $controller_path ) ");
if (isset($panel_path)) {
$controller_path = '\\'.$panel_path.'\\'.$entity.'Controller';
} else {
$controller_path = '';
$finder = new Finder();
$files = $finder->files()->name($entity."Controller.php")->in(
\App::basePath().'/app'
);
foreach ($files as $item) {
$fileContent = $item->getContents();
$controller_path = $this->getNameSpace(
$fileContent
)."\\".$entity."Controller";
}
if ($controller_path === '') {
throw new \Exception("Controller not found ( $entity.\"Controller.php\" ) ");
}
}
}

try {
$controller = \App::make($controller_path);
} catch (\Exception $ex) {
throw new \Exception("Controller not found ( $controller_path ) ");
}

if (!method_exists($controller, $methods)) {
throw new \Exception(
'Controller does not implement the CrudController methods!'
);
} else {
return $controller->callAction(
$methods,
array('entity' => $entity)
);
}

}

if (!method_exists($controller, $methods)){
throw new \Exception('Controller does not implement the CrudController methods!');
} else {
return $controller->callAction($methods, array('entity' => $entity));
private function getNameSpace($fileContent)
{
if (preg_match('#^namespace\s+(.+?);$#sm', $fileContent, $m)) {
return $m[1];
}
return null;
}

}
}