Skip to content

Commit 1486d5f

Browse files
committed
Fix Attributes pb => use not added for action creation
1 parent af2b675 commit 1486d5f

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/Ubiquity/scaffolding/ScaffoldController.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,24 +165,40 @@ public function _newAction($controller, $action, $parameters = null, $content =
165165
if ($classCode !== false) {
166166
$fileContent = \implode('', $classCode);
167167
$fileContent = \trim($fileContent);
168-
$posLast = \strrpos($fileContent, '}');
169-
if ($posLast !== false) {
170168
if ($createView) {
171169
$viewname = $this->_createViewOp(ClassUtils::getClassSimpleName($controller), $action, $theme);
172170
$content .= "\n\t\t\$this->loadView('" . $viewname . "');\n";
173171
$msgContent .= "<br>Created view : <b>" . $viewname . "</b>";
174172
}
175173
$routeAnnotation = $this->generateRouteAnnotation($routeInfo);
174+
176175
if ($routeAnnotation != '') {
177176
$msgContent .= $this->_addMessageForRouteCreation($routeInfo["path"]);
177+
if(\count($this->getUses())>0){
178+
$namespace='namespace '.$r->getNamespaceName().";\n";
179+
$posUses = \strpos($fileContent, $namespace);
180+
if($posUses!==false){
181+
$posUses+=strlen($namespace);
182+
$uses=$this->uses;
183+
foreach ($uses as $use=>$_){
184+
if(\strpos($fileContent, 'use '.$use)!==false){
185+
unset($this->uses[$use]);
186+
}
187+
}
188+
if(\count($this->getUses())>0){
189+
$fileContent = \substr_replace($fileContent, "\n".$this->getUsesStr(), $posUses - 1, 0);
190+
}
191+
}
192+
}
178193
}
179194
$parameters = CodeUtils::cleanParameters($parameters);
180195
$actionContent = UFileSystem::openReplaceInTemplateFile($templateDir . "action.tpl", [
181-
'%route%' => "\n" . $routeAnnotation ?? '',
196+
'%route%' => "\n\t" . $routeAnnotation ?? '',
182197
'%actionName%' => $action,
183198
'%parameters%' => $parameters,
184199
'%content%' => $content
185200
]);
201+
$posLast = \strrpos($fileContent, '}');
186202
$fileContent = \substr_replace($fileContent, "\n%content%", $posLast - 1, 0);
187203
if (! CodeUtils::isValidCode('<?php ' . $content)) {
188204
echo $this->showSimpleMessage("Errors parsing action content!", "warning", "Creation", "warning circle", null, "msgControllers");
@@ -195,7 +211,7 @@ public function _newAction($controller, $action, $parameters = null, $content =
195211
echo $this->showSimpleMessage($msgContent, "success", "Creation", "info circle", null, "msgControllers");
196212
}
197213
}
198-
}
214+
199215
} else {
200216
echo $this->showSimpleMessage("The action {$action} already exists in {$controller}!", "error", "Creation", "warning circle", null, "msgControllers");
201217
}
@@ -228,7 +244,7 @@ protected function generateRouteAnnotation($routeInfo) {
228244
}
229245

230246

231-
return CacheManager::getAnnotationsEngineInstance()->getAnnotation($this,$name,$routeProperties)->asAnnotation();;
247+
return CacheManager::getAnnotationsEngineInstance()->getAnnotation($this,$name,$routeProperties)->asAnnotation();
232248
}
233249
return '';
234250
}

0 commit comments

Comments
 (0)