Skip to content

Commit

Permalink
Backport Prefix support for Controllers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Aug 18, 2020
1 parent 5a2c006 commit dfd2eda
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/Annotator/ControllerAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function annotate($path) {

$annotations = $this->_getModelAnnotations($usedModels, $content);

$componentAnnotations = $this->_getComponentAnnotations($className);
$componentAnnotations = $this->_getComponentAnnotations($className, $path);
foreach ($componentAnnotations as $componentAnnotation) {
$annotations[] = $componentAnnotation;
}
Expand Down Expand Up @@ -107,11 +107,12 @@ protected function _getUsedModels($content) {

/**
* @param string $controllerName
* @param string $path
* @return \IdeHelper\Annotation\AbstractAnnotation[]
*/
protected function _getComponentAnnotations($controllerName) {
protected function _getComponentAnnotations($controllerName, $path) {
try {
$map = $this->_getUsedComponents($controllerName);
$map = $this->_getUsedComponents($controllerName, $path);
} catch (Exception $e) {
if ($this->getConfig(static::CONFIG_VERBOSE)) {
$this->_io->warn(' Skipping component annotations: ' . $e->getMessage());
Expand Down Expand Up @@ -140,22 +141,25 @@ protected function _getComponentAnnotations($controllerName) {

/**
* @param string $controllerName
* @param string $path
*
* @return string[]
*/
protected function _getUsedComponents($controllerName) {
protected function _getUsedComponents($controllerName, $path) {
$plugin = $controllerName !== 'AppController' ? $this->getConfig(static::CONFIG_PLUGIN) : null;
$className = App::className(($plugin ? $plugin . '.' : '') . $controllerName, 'Controller');
if (!$className) {
$prefix = $this->getPrefix($controllerName, $path);

$fullClassName = App::className(($plugin ? $plugin . '.' : '') . $controllerName, 'Controller' . $prefix);
if (!$fullClassName) {
return [];
}

if ($this->_isAbstract($className)) {
if ($this->_isAbstract($fullClassName)) {
return [];
}

/** @var \App\Controller\AppController $controller */
$controller = new $className();
$controller = new $fullClassName();

$components = [];
foreach ($controller->components()->loaded() as $component) {
Expand All @@ -166,7 +170,7 @@ protected function _getUsedComponents($controllerName) {
return $components;
}

$appControllerComponents = $this->_getUsedComponents('AppController');
$appControllerComponents = $this->_getUsedComponents('AppController', $path);
$components = array_diff_key($components, $appControllerComponents);

return $components;
Expand Down Expand Up @@ -308,4 +312,22 @@ protected function _findModelClass($className, $path) {
return $modelClass;
}

/**
* Namespace prefix for controllers.
*
* @param string $className
* @param string $path
*
* @return string
*/
protected function getPrefix($className, $path) {
preg_match('#/Controller/(\w+)/' . $className . '\.php#', $path, $matches);
$prefix = '';
if ($matches) {
$prefix = '/' . $matches[1];
}

return $prefix;
}

}

0 comments on commit dfd2eda

Please sign in to comment.