-
-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Maker; | ||
|
||
use Symfony\Bundle\MakerBundle\ConsoleStyle; | ||
use Symfony\Bundle\MakerBundle\DependencyBuilder; | ||
use Symfony\Bundle\MakerBundle\Generator; | ||
use Symfony\Bundle\MakerBundle\InputConfiguration; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\WebpackEncoreBundle\WebpackEncoreBundle; | ||
|
||
/** | ||
* @author Abdelilah Jabri <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class MakeStimulusController extends AbstractMaker | ||
{ | ||
const SUFFIX = '_controller.js'; | ||
|
||
const PATH = 'assets/controllers/'; | ||
|
||
public static function getCommandName(): string | ||
{ | ||
return 'make:stimulus-controller'; | ||
} | ||
|
||
public static function getCommandDescription(): string | ||
{ | ||
return 'Creates a new Stimulus controller'; | ||
} | ||
|
||
public function configureCommand(Command $command, InputConfiguration $inputConfig) | ||
{ | ||
$command | ||
->addArgument('name', InputArgument::OPTIONAL, 'The name of the stimulus controller (e.g. <fg=yellow>hello</>)') | ||
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeStimulusController.txt')); | ||
} | ||
|
||
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) | ||
{ | ||
$controllerName = $input->getArgument('name'); | ||
$fileName = $controllerName.self::SUFFIX; | ||
$filePath = self::PATH.$fileName; | ||
|
||
$generator->generateFile( | ||
$filePath, | ||
'stimulus/Controller.tpl.php' | ||
); | ||
|
||
$generator->writeChanges(); | ||
|
||
$this->writeSuccessMessage($io); | ||
|
||
$io->text([ | ||
'Next:', | ||
sprintf('- Open <info>%s</info> and add the code you need', $filePath), | ||
sprintf('- Use the controller in your template as <info><div data-controller="%s"></div></info>', $controllerName), | ||
'Find the documentation at <fg=yellow>https://github.com/symfony/stimulus-bridge</>', | ||
]); | ||
} | ||
|
||
public function configureDependencies(DependencyBuilder $dependencies) | ||
{ | ||
$dependencies->addClassDependency( | ||
WebpackEncoreBundle::class, | ||
'webpack-encore-bundle' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The <info>%command.name%</info> command generates new Stimulus Controller. | ||
|
||
<info>php %command.full_name% hello</info> | ||
|
||
If the argument is missing, the command will ask for the controller name interactively. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
export default class extends Controller { | ||
connect() { | ||
// Here: update the connected element | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Tests\Maker; | ||
|
||
use Symfony\Bundle\MakerBundle\Maker\MakeStimulusController; | ||
use Symfony\Bundle\MakerBundle\Test\MakerTestCase; | ||
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner; | ||
|
||
class MakeStimulusControllerTest extends MakerTestCase | ||
{ | ||
protected function getMakerClass(): string | ||
{ | ||
return MakeStimulusController::class; | ||
} | ||
|
||
public function getTestDetails() | ||
{ | ||
yield 'it_generates_stimulus_controller' => [$this->createMakerTest() | ||
->run(function (MakerTestRunner $runner) { | ||
$runner->runMaker( | ||
[ | ||
// controller name | ||
'custom', | ||
]); | ||
|
||
$this->assertFileExists($runner->getPath('assets/controllers/custom_controller.js')); | ||
}), | ||
]; | ||
} | ||
} |