Skip to content

Commit

Permalink
add draft for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Jul 26, 2023
1 parent 0f6cb6a commit c7aec4a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/web/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace yii1tech\di\web;

/**
* {@inheritdoc}
*/
class Controller extends \CController
{
use CreatesActionViaDI;
}
65 changes: 65 additions & 0 deletions src/web/CreatesActionViaDI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace yii1tech\di\web;

use CException;
use Yii;
use yii1tech\di\DI;

/**
* @mixin \CController
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
trait CreatesActionViaDI
{
/**
* {@inheritdoc}
*/
public function createAction($actionID)
{
if ($actionID === '') {
$actionID = $this->defaultAction;
}

if (method_exists($this, 'action' . $actionID) && strcasecmp($actionID, 's')) {// we have actions method
return new InlineAction($this, $actionID);
}

return parent::createAction($actionID);
}

/**
* {@inheritdoc}
*/
protected function createActionFromMap($actionMap, $actionID, $requestActionID, $config = [])
{
if(($pos = strpos($actionID, '.')) === false && isset($actionMap[$actionID])) {
$config = array_merge(
is_array($actionMap[$actionID]) ? $actionMap[$actionID] : ['class' => $actionMap[$actionID]],
$config
);

if (isset($config['class'])) {
$class = $config['class'];
unset($config['class']);
} else {
throw new CException(Yii::t('yii', 'Object configuration must be an array containing a "class" element.'));
}

$action = DI::make($class, [
'controller' => $this,
'id' => $requestActionID,
]);

foreach ($config as $name => $value) {
$action->$name = $value;
}

return $action;
}

return parent::createActionFromMap($actionMap, $actionID, $requestActionID, $config);
}
}
11 changes: 11 additions & 0 deletions src/web/InlineAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace yii1tech\di\web;

/**
* {@inheritdoc}
*/
class InlineAction extends \CInlineAction
{

}

0 comments on commit c7aec4a

Please sign in to comment.