PHP code example of asika / php-code-generator

1. Go to this page and download the library: Download asika/php-code-generator 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/ */

    

asika / php-code-generator example snippets

 shell
php composer.phar create-project asika/php-code-generator php-code-generator 2.*
 shell
php bin/generator
 bash
php bin/generator gen acme test/MyApp
 bash
$ php bin/generator gen acme test/MyApp
File created: /var/www/php-code-generator/test/MyApp/admin/article/edit.twig
File created: /var/www/php-code-generator/test/MyApp/admin/article/index.twig
File created: /var/www/php-code-generator/test/MyApp/admin/category/edit.twig
File created: /var/www/php-code-generator/test/MyApp/admin/category/index.twig
File created: /var/www/php-code-generator/test/MyApp/article.twig
File created: /var/www/php-code-generator/test/MyApp/global/index.html
File created: /var/www/php-code-generator/test/MyApp/index.html
File created: /var/www/php-code-generator/test/MyApp/index.twig
 bash
php bin/generator gen acme test/MyApp2 -t mytmpl
 bash
php bin/generator tmpl-init flower
 php
protected $tagVariable = array('{@', '@}');

protected function registerReplaces($io, $replace = array())
{
    $item = $io->getOption('n', 'sakura');

    /*
     * Replace with your code name.
     */

    // Set item name, default is sakura
    $replace['item.lower'] = strtolower($item);
    $replace['item.upper'] = strtoupper($item);
    $replace['item.cap']   = ucfirst($item);

    // Set project name
    $replace['project.class'] = 'CodeGenerator';

    return $replace;
}
 php
protected function registerConfig($io, $config)
{
    /*
     * Replace with your project path.
     */

    $subTemplate = $io->getOption('t', 'default');
    $dest        = $io->getArgument(1) ? : 'generated';

    $config['path.src']  = __DIR__ . '/Template/' . $subTemplate;
    $config['path.dest'] = GENERATOR_PATH . '/' . $dest;

    return $config;
}
 php
namespace FlowerTemplate\Task;

use FlowerTemplate\Action;
use CodeGenerator\Controller\AbstractTaskController;

class Generate extends AbstractTaskController
{
	public function execute()
	{
		$this->doAction(new Action\CopyAllAction);
	}
}
 php
namespace FlowerTemplate\Action;

use CodeGenerator\Action\AbstractAction;
use CodeGenerator\FileOperator\CopyOperator;

class CopyAllAction extends AbstractAction
{
	protected function doExecute()
	{
		$copyOperator = new CopyOperator($this->io, (array) $this->config['tag.variable']);

		$copyOperator->copy($this->config['path.src'], $this->config['path.dest'], $this->config['replace']);
	}
}
 php
class Generate extends AbstractTaskController
{
	public function execute()
	{
		$this->doAction(new Action\CopyAllAction);

		$this->doAction(new Action\ImportSqlAction);

		$this->doAction(new Action\Github\CloneSomeRepoAction);

		$this->doAction(new Action\User\CreateNewUserAction);
	}
}
 php
$copyOperator = new CopyOperator($this->io, array('{@', '@}'));

$copyOperator->copy($src, $dest, $replaceArray);
 php
namespace CodeGenerator\Filesystem;

Filesystem\Folder::copy($src, $dest);
Filesystem\Folder::move($src, $dest);
Filesystem\Folder::create($path);
Filesystem\Folder::delete($path);

Filesystem\File::copy($src, $dest);
Filesystem\File::move($src, $dest);
Filesystem\File::write($path, $buffer);
Filesystem\File::delete($path);

// Replace / and \ to DIRECTORY_SEPARATOR
$path = Filesystem\Path::clean($path);
 php
protected function registerCommands()
{
    $this->addCommand(new Command\Generate\Generate);
    $this->addCommand(new Command\Init\Init);
    $this->addCommand(new Command\Convert\Convert);

    // Add here
    $this->addCommand(new Command\MyTask\Task);
}
 php
namespace FlowerTemplate\Task;

use FlowerTemplate\Action;
use CodeGenerator\Controller\TaskController;

class MyTask extends TaskController
{
	public function execute()
	{
		$this->doAction(new Action\CopyAllAction);
	}
}
 bash
php bin/generator mytask <arguments>
 php
$controller = new GeneratorController(new MyIOAdapter($input, $output));

$controller->setTask($input->getArgument('task'))->execute();