Created
September 19, 2017 13:08
-
-
Save MaximStrutinskiy/6a0c07375c267a7789b1e6725e53d731 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace App\Command; | |
use App\Entity\User; | |
use Knp\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class GenerateUser extends Command | |
{ | |
private $app; | |
public function __construct($app, $name = null) | |
{ | |
parent::__construct($name); | |
$this->app = $app; | |
} | |
protected function configure() | |
{ | |
$this | |
->setName("app:generate:user") | |
->setDescription("Generate User. Set arguments: email, name, password, role. [email(string), name(string), password(string), role(bool) = 'admin' or 'user']") | |
->setDefinition([ | |
new InputArgument( | |
'email', InputArgument::REQUIRED, 'Enter user Email.' | |
), | |
new InputArgument( | |
'name', InputArgument::REQUIRED, 'Enter user Name.' | |
), | |
new InputArgument( | |
'password', InputArgument::REQUIRED, 'Enter user Password.' | |
), | |
new InputArgument( | |
'role', InputArgument::REQUIRED, 'Enter user Role.' | |
), | |
]); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$em = $this->app['orm.em']; | |
$email = $input->getArgument('email'); | |
$name = $input->getArgument('name'); | |
$password = $input->getArgument('password'); | |
$role = $input->getArgument('role'); | |
if (($role != 'user') && (($role != 'admin'))) { | |
throw new \InvalidArgumentException( | |
sprintf("[Role FieldError] " . $role . " role is note define. Select 'admin' or 'user' role.") | |
); | |
} | |
$user = new User(); | |
$user->setEmail($email); | |
$user->setUsername($name); | |
if ($role == 'user') { | |
$user->setRoles('ROLE_USER'); | |
} elseif ($role == 'admin') { | |
$user->setRoles('ROLE_ADMIN'); | |
} | |
$user->setPassword($this->app['encode_password']($user, $password)); | |
$user->setEnable(true); | |
$user->setCreatedAt(new \DateTime('now', new \DateTimeZone('Europe/Kiev'))); | |
$user->setUpdatedAt(new \DateTime('now', new \DateTimeZone('Europe/Kiev'))); | |
$em->persist($user); | |
$em->flush(); | |
$output->writeln("User has been created!"); | |
} | |
} |
This file contains hidden or 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
<?php | |
require_once dirname(__DIR__) . '/vendor/autoload.php'; | |
/** @var boolean $cli If the bootstrap (/web/index.php) sees this variable, don't 'run' the HTTP front-controller */ | |
$cli = true; | |
require_once dirname(__DIR__) . '/web/index.php'; | |
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain, | |
Symfony\Component\Console\Application as CliApplication, | |
Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper, | |
Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper, | |
Doctrine\Common\Annotations\AnnotationRegistry, | |
Doctrine\ORM\Mapping\Driver\AnnotationDriver, | |
Doctrine\Common\Annotations\AnnotationReader, | |
Symfony\Component\Console\Helper\HelperSet, | |
Doctrine\ORM\Mapping\Driver\DatabaseDriver, | |
Doctrine\ORM\Tools\Console\Command, | |
Doctrine\DBAL\Connection, | |
Doctrine\DBAL\Version; | |
/** @var Connection $db The above bootstrap creates the app object for us */ | |
$db = $app['db']; | |
/** @var Doctrine\ORM\EntityManager $em The entity manager */ | |
$em = $app['orm.em']; | |
$driver = new DatabaseDriver($db->getSchemaManager()); | |
$driver->setNamespace('App\\Entity\\'); | |
$annotationsFile = dirname(__DIR__) . '/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'; | |
AnnotationRegistry::registerFile($annotationsFile); | |
$driverChain = new MappingDriverChain(); | |
$driverChain->addDriver( | |
new AnnotationDriver(new AnnotationReader(), [dirname(__DIR__) . '/src/App/Entity']), | |
'App\Entity\\' | |
); | |
$em->getConfiguration()->setMetadataDriverImpl($driverChain); | |
/** @var Symfony\Component\Console\Application $cli */ | |
$cli = new CliApplication('Doctrine Command Line Interface', Version::VERSION); | |
$cli->setCatchExceptions(true); | |
$cli->setHelperSet( | |
new HelperSet( | |
[ | |
'db' => new ConnectionHelper($em->getConnection()), | |
'em' => new EntityManagerHelper($em), | |
] | |
) | |
); | |
/** | |
* Add Doctrine commands | |
* Add more commands vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/Command/ | |
**/ | |
$cli->addCommands( | |
[ | |
new Command\GenerateRepositoriesCommand, | |
new Command\GenerateEntitiesCommand, | |
new Command\ConvertMappingCommand, | |
new Command\ValidateSchemaCommand, | |
new Command\SchemaTool\CreateCommand, | |
new Command\SchemaTool\UpdateCommand, | |
new Command\GenerateProxiesCommand, | |
new \App\Command\GenerateUser($app), | |
] | |
); | |
$cli->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment