1. Go to this page and download the library: Download webchemistry/stimulus library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webchemistry / stimulus example snippets
// directory with controllers, only *_controller.js and *-controller.js are extracted
$extractor = new JavascriptSourceExtractor(__DIR__ . '/js/stimulus/controllers');
// optional, we want UI namespace instead of Ui
$keywords = ['ui' => 'UI'];
// generate classes as Stimulus\*\_*Controller, these classes we don't edit
$originalClassNameConverter = new PrependClassNameConverter(
'Stimulus\\',
new AppendClassNameConverter(
new BaseClassNameConverter($keywords, fn (string $className) => '_' . $className),
'Controller',
),
);
// We want edit these classes
$emptyClassNameConverter = new PrependClassNameConverter(
'App\\Stimulus\\Controller\\',
new AppendClassNameConverter(
new BaseClassNameConverter($keywords),
'Controller',
),
);
// for autocomplete_controller.js these controllers are generated
// class Stimulus\_AutocompleteController
// class App\Stimulus\Controller\AutocompleteController extends Stimulus\_AutocompleteController
// for namespace/autocomplete_controller.js these controllers are generated
// class Stimulus\Namespace\_AutocompleteController
// class App\Stimulus\Controller\Namespace\AutocompleteController extends Stimulus\_AutocompleteController
// Generator generates static methods
$generator = new StaticClassStimulusControllerGenerator($extractor, $originalClassNameConverter);
// Generator generates class with empty body and extends original class (Stimulus\*\_*Controller)
$emptyGenerator = new EmptyClassStimulusControllerGenerator(
$extractor,
$emptyClassNameConverter,
$originalClassNameConverter,
);
// Files are written in app/generated/stimulus/*, Stimulus\ namespace have to be removed from path
$writer = new FilesystemWriter(__DIR__ . '/app/generated/stimulus', 'Stimulus\\');
// Files are written in app/src/Stimulus/Controller/*, App\Stimulus\Controller\ namespace have to be removed from path, if file exists don't rewrite it
$emptyWriter = new FilesystemWriter(__DIR__ . '/app/src/Stimulus/Controller', 'App\\Stimulus\\Controller\\', false);
foreach ($generator->generate() as $generated) {
$writer->write($generated);
}
foreach ($emptyGenerator->generate() as $generated) {
$emptyWriter->write($generated);
}
$extractor = new JavascriptSourceExtractor(__DIR__ . '/js/stimulus/controllers');
$keywords = ['ui' => 'UI'];
$originalClassNameConverter = new PrependClassNameConverter(
'Stimulus\\',
new AppendClassNameConverter(
new BaseClassNameConverter($keywords, fn (string $className) => '_' . $className),
'Controller',
),
);
$emptyClassNameConverter = new PrependClassNameConverter(
'App\\Stimulus\\Controller\\',
new AppendClassNameConverter(
new BaseClassNameConverter($keywords),
'Controller',
),
);
$generator = new StaticClassStimulusControllerGenerator($extractor, $originalClassNameConverter);
$emptyGenerator = new EmptyClassStimulusControllerGenerator(
$extractor,
$emptyClassNameConverter,
$originalClassNameConverter,
);
$writer = new FilesystemWriter(__DIR__ . '/app/generated/stimulus', 'Stimulus\\');
$emptyWriter = new FilesystemWriter(__DIR__ . '/app/src/Stimulus/Controller', 'App\\Stimulus\\Controller\\', false);
foreach ($generator->generate() as $generated) {
$writer->write($generated);
}
foreach ($emptyGenerator->generate() as $generated) {
$emptyWriter->write($generated);
}
declare(strict_types = 1);
/**
* NOTE: This class is auto generated by file: my_controller.js
* Do not edit the class manually
*/
namespace Stimulus;
use WebChemistry\Stimulus\Type\StimulusAction;
use WebChemistry\Stimulus\Type\StimulusController;
use WebChemistry\Stimulus\Type\StimulusTarget;
abstract class _MyController
{
final public const identifier = 'my';
public static function construct(string $stringValue, string $activeClass): StimulusController
{
return new StimulusController(self::identifier, [
'stringValue' => $stringValue,
'activeClass' => $activeClass,
], []);
}
public static function itemTarget(): StimulusTarget
{
return new StimulusTarget(self::identifier, 'itemTarget');
}
public static function switchAction(): StimulusAction
{
return new StimulusAction(self::identifier, 'switch', []);
}
}
use WebChemistry\Stimulus\Renderer\HtmlRenderer;
$htmlAttributes = HtmlRenderer::render(
App\Stimulus\Controller\MyController::construct('string', 'activeClass'),
App\Stimulus\Controller\MyController::switchAction()->event('click'),
); // data-controller="my" data-my-string-value="string" data-my-active-class="active" data-action="click->my#switch"
// or as array [attribute => value]
HtmlRenderer::toArray(...);
declare(strict_types = 1);
namespace App\Stimulus\Controller;
use Stimulus\_MyController as ParentController;
use WebChemistry\Stimulus\Type\StimulusController;
final class MyController extends ParentController
{
public function __construct(
private LinkGenerator $linkGenerator,
) {}
public function doConstruct(): StimulusController
{
return self::construct($this->linkGenerator->link('...'));
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.