PHP code example of zachleigh / yarak

1. Go to this page and download the library: Download zachleigh/yarak 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/ */

    

zachleigh / yarak example snippets


use Yarak\Kernel;

$di->setShared('yarak',function () {
    return new Kernel();
});

'database' => [
    'adapter'     => '',
    'host'        => '',
    'username'    => '',
    'password'    => '',
    'dbname'      => '',
    'charset'     => '',
],
'application' => [
    'appDir'         => APP_PATH.'/',
    'commandsDir'    => APP_PATH.'/console/commands',
    'consoleDir'     => APP_PATH.'/console/',
    'databaseDir'    => APP_PATH.'/database/',
    'migrationsDir'  => APP_PATH.'/database/migrations/',
    'modelsDir'      => APP_PATH.'/models/',
],
'namespaces' => [
    'root' => '',
],

'yarak' => [
    'database' => [
        'adapter'     => '',
        'host'        => '',
        'username'    => '',
        'password'    => '',
        'dbname'      => '',
        'charset'     => '',
    ],
    'application' => [
        'appDir'         => APP_PATH.'/',
        'commandsDir'    => APP_PATH.'/console/commands',
        'consoleDir'     => APP_PATH.'/console/',
        'databaseDir'    => APP_PATH.'/database/',
        'migrationsDir'  => APP_PATH.'/database/migrations/',
        'modelsDir'      => APP_PATH.'/models/',
    ],
    'namespaces' => [
        'root' => '',
    ],
]

$di->setShared('yarak',function () {
    return new Kernel('yarak');
});

'services' => [
    'yarak' => [
        'database' => [
            'adapter'     => '',
            'host'        => '',
            'username'    => '',
            'password'    => '',
            'dbname'      => '',
            'charset'     => '',
        ],
        'application' => [
            'appDir'         => APP_PATH.'/',
            'commandsDir'    => APP_PATH.'/console/commands',
            'consoleDir'     => APP_PATH.'/console/',
            'databaseDir'    => APP_PATH.'/database/',
            'migrationsDir'  => APP_PATH.'/database/migrations/',
            'modelsDir'      => APP_PATH.'/models/',
        ],
        'namespaces' => [
            'root' => '',
        ],
    ]
]

$di->setShared('yarak',function () {
    return new Kernel('services.yarak');
});

$di->setShared('yarak',function () {
    return new Kernel([
        'namespaces' => [
            'root' => '',
        ],
    ]);
});

$di->setShared('yarak',function () {
    return new Kernel([
        'database' => [
            'adapter'     => '',
            'host'        => '',
            'username'    => '',
            'password'    => '',
            'dbname'      => '',
            'charset'     => '',
        ],
        'application' => [
            'appDir'         => APP_PATH.'/',
            'commandsDir'    => APP_PATH.'/console/commands',
            'consoleDir'     => APP_PATH.'/console/',
            'databaseDir'    => APP_PATH.'/database/',
            'migrationsDir'  => APP_PATH.'/database/migrations/',
            'modelsDir'      => APP_PATH.'/models/',
        ],
        'namespaces' => [
            'root' => '',
        ],
    ], false);
});

#!/usr/bin/env php


use Phalcon\Di\FactoryDefault;

error_reporting(E_ALL);

define('BASE_PATH', __DIR__);
define('APP_PATH', BASE_PATH . '/app');

/*
|--------------------------------------------------------------------------
| Autoload The Application
|--------------------------------------------------------------------------
|
| In order to work properly, Yarak will need both your project files and the
| vendor folder to be autoloaded.
|
*/
-------------
| Handle The Incoming Commands
|--------------------------------------------------------------------------
|
| We'll get the Yarak kernel from the dependency injector and defer to it for 
| command handling.
|
*/
$kernel = $di->getYarak();

$kernel->handle();

public function define($class, callable $attributes, $name = 'default')

use App\Models\Users;

$factory->define(Users::class, function (Faker\Generator $faker) {
    return [
        'username' => $faker->userName,
        'email' => $faker->unique()->safeEmail,
        'password' => 'password',
    ];
});

use App\Models\Users;

$factory->define(Users::class, function (Faker\Generator $faker) {
    return [
        'username' => 'myUsername',
        'email' => 'myEmail',
        'password' => 'myPassword',
    ];
}, 'myUser');

use App\Models\Users;

$factory->define(Users::class, function (Faker\Generator $faker) use ($factory) {
    return [
        'username' => $faker->userName,
        'email' => $faker->unique()->safeEmail,
        'password' => $factory->security->hash('password'),
    ];
});

use App\Models\Users;

$user = factory(Users::class)->make();

use App\Models\Users;

$user = factory(Users::class)->create();

use App\Models\Users;

// Make three users
$users = factory(Users::class, 3)->make();

// Create three users
$users = factory(Users::class, 3)->create();

use App\Models\Users;

// Make a user with username 'bobsmith' and email '[email protected]'
$user = factory(Users::class)->make([
    'username' => 'bobsmith',
    'email'    => '[email protected]'
]);

// Create a user with username 'bobsmith' and email '[email protected]'
$user = factory(Users::class)->create([
    'username' => 'bobsmith',
    'email'    => '[email protected]'
]);

use App\Models\Users;

// Make a user using the factory named 'myUser'
factory(Users::class, 'myUser')->make()

// Create a user using the factory named 'myUser'
factory(Users::class, 'myUser')->create()

use App\Models\Users;

// Make three users using the factory named 'myUser'
$users = factory(Users::class, 'myUser', 3)->make();

// Create three users using the factory named 'myUser'
$users = factory(Users::class, 'myUser', 3)->creates();

$connection->createTable(
    'posts',
    null,
    [
        'columns' => [
            new Column('id', [
                'type'          => Column::TYPE_INTEGER,
                'size'          => 10,
                'unsigned'      => true,
                'notNull'       => true,
                'autoIncrement' => true,
            ]),
            new Column('title', [
                'type'    => Column::TYPE_VARCHAR,
                'size'    => 200,
                'notNull' => true,
            ]),
            new Column('body', [
                'type'    => Column::TYPE_TEXT,
                'notNull' => true,
            ]),
            new Column('users_id', [
                'type'     => Column::TYPE_INTEGER,
                'size'     => 10,
                'unsigned' => true,
                'notNull'  => true,
            ]),
            new Column('created_at', [
                'type'    => Column::TYPE_TIMESTAMP,
                'notNull' => true,
                'default' => 'CURRENT_TIMESTAMP',
            ]),
        ],
        'indexes' => [
            new Index('PRIMARY', ['id'], 'PRIMARY')
        ],
        'references' => [
            new Reference(
                'user_idfk',
                [
                    'referencedTable'   => 'users',
                    'columns'           => ['users_id'],
                    'referencedColumns' => ['id'],
                ]
            ),
        ],
    ]
);

use App\Models\Posts;
use App\Models\Users;

$factory->define(Users::class, function (Faker\Generator $faker) use ($factory) {
    return [
        'username' => $faker->userName,
        'email'    => $faker->unique()->safeEmail,
        'password' => $factory->security->hash('password'),
    ];
});

$factory->define(Posts::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->unique()->sentence(4, true),
        'body'  => $faker->paragraph(4, true),
    ];
});

use App\Models\Posts;
use App\Models\Users;

$users = factory(Users::class, 3)->create();

foreach ($users as $user) {
    factory(Posts::class)->create([
        'users_id' => $user->id
    ]);
}

use App\Models\Posts;
use App\Models\Users;

$users = factory(Users::class, 3)->create();

foreach ($users as $user) {
    factory(Posts::class, 3)->create([
        'users_id' => $user->id
    ]);
}

use App\Models\Posts;
use App\Models\Users;

$factory->define(Users::class, function (Faker\Generator $faker) use ($factory) {
    return [
        'username' => $faker->userName,
        'email'    => $faker->unique()->safeEmail,
        'password' => $factory->security->hash('password'),
    ];
});

$factory->define(Posts::class, function (Faker\Generator $faker) {
    return [
        'title'    => $faker->unique()->sentence(4, true),
        'body'     => $faker->paragraph(4, true),
        'users_id' => function () {
            return factory(Users::class)->create()->id;
        }
    ];
}, 'withUser');

use App\Models\Posts;

factory(Posts::class, 'withUser', 20)->create();

use App\Models\Users;
use Yarak\DB\Seeders\Seeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(Users::class, 5)->create();
    }
}

use App\Models\Posts;
use App\Models\Users;
use Yarak\DB\Seeders\Seeder;

class PostsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $allUsers = Users::find();

        foreach ($allUsers as $user) {
            factory(Posts::class, 5)->create(['users_id' => $user->getId()]);
        }
    }
}

use Yarak\DB\Seeders\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(PostsTableSeeder::class);
    }
}

public createTable (mixed $tableName, mixed $schemaName, array $definition)

use Phalcon\Db\Index;
use Phalcon\Db\Column;

//

public function up(Pdo $connection)
{
    $connection->createTable(
        'users',
        null,
        [
            'columns' => [
                new Column('id', [
                    'type'          => Column::TYPE_INTEGER,
                    'size'          => 10,
                    'unsigned'      => true,
                    'notNull'       => true,
                    'autoIncrement' => true
                ]),
                new Column('username', [
                    'type'    => Column::TYPE_VARCHAR,
                    'size'    => 32,
                    'notNull' => true
                ]),
                new Column('password', [
                    'type'    => Column::TYPE_CHAR,
                    'size'    => 40,
                    'notNull' => true
                ]),
                new Column('email', [
                    'type'    => Column::TYPE_VARCHAR,
                    'size'    => 20,
                    'notNull' => true
                ]),
                new Column('created_at', [
                    'type'    => Column::TYPE_TIMESTAMP,
                    'notNull' => true,
                    'default' => 'CURRENT_TIMESTAMP'
                ])
            ],
            'indexes' => [
                new Index('PRIMARY', ['id'], 'PRIMARY'),
                new Index('users_username_unique', ['username'], 'UNIQUE'),
                new Index('users_email_unique', ['email'], 'UNIQUE')
            ]
        ]
    );
}

public modifyColumn (mixed $tableName, mixed $schemaName, Phalcon\Db\ColumnInterface $column, [Phalcon\Db\ColumnInterface $currentColumn])

public function up(Pdo $connection)
{
    $connection->modifyColumn(
        'users',
        null,
        new Column(
            'email',
            [
                'type' => Column::TYPE_VARCHAR,
                'size' => 70,
            ]
        )
    );
}

public addColumn (mixed $tableName, mixed $schemaName, Phalcon\Db\ColumnInterface $column)

public function up(Pdo $connection)
{
    $connection->addColumn(
        'users',
        null,
        new Column(
            'active',
            [
                'type'    => Column::TYPE_CHAR,
                'size'    => 1,
                'notNull' => true,
            ]
        )
    );
}

public function down(Pdo $connection)
{
    $connection->dropTable('users');
}

public function down(Pdo $connection)
{
    $connection->modifyColumn(
        'users',
        null,
        new Column(
            'email',
            [
                'type' => Column::TYPE_VARCHAR,
                'size' => 20,
            ]
        )
    );
}

public function down(Pdo $connection)
{
    $connection->dropColumn('users', null, 'active');
}

'application' => [
    //
    'consoleDir' => APP_PATH.'/console/'
],
'namespaces' => [
    //
    'console' => 'App\Console'
],
});

namespace App\Console\Commands;

use Yarak\Console\Command;

class ExampleCommand extends Command
{
    /**
     * The command signature.
     *
     * @var string
     */
    protected $signature = 'namespace:name {argument} {--o|option=default}';

    /**
     * The command description.
     *
     * @var string
     */
    protected $description = 'Command decription.';

    /**
     * Handle the command.
     */
    protected function handle()
    {
        // handle the command
    }
}

protected $signature = 'namespace:name';

protected $signature = 'namespace:name {arg} {--option}'

protected $signature = 'namespace:name {arg?} {--option}'

protected $signature = 'namespace:name {arg=default} {--option}'

protected $signature = 'namespace:name {arg*} {--option}'

protected $signature = 'namespace:name {arg?*} {--option}'

protected $signature = 'namespace:name {arg=default : Argument description} {--option}'

protected $signature = 'namespace:name {argument} {--opt}'

protected $signature = 'namespace:name {argument} {--opt=}'

protected $signature = 'namespace:name {argument} {--opt=default}'

protected $signature = 'namespace:name {argument} {--o|opt}'

protected $signature = 'namespace:name {argument} {--opt=*}'

protected $signature = 'namespace:name {argument} {--o|opt : option description.}'

protected function handle()
{
    $arg = $this->argument('arg'); // passed value of arg

    $allArguments = $this->argument(); // array of all arguments
}

protected function handle()
{
    $opt = $this->option('opt'); // passed value of opt

    $allOptions = $this->option(); // array of all options
}

protected function handle()
{
    $argExists = $this->hasArgument('exists');  // true

    $optExists = $this->hasOption('doesntExist');  // false
}

if ($this->confirm('Do you wish to continue? ')) {
    //
}

$name = $this->ask('What is your name?', 'Nobody');

$password = $this->askPassword('Please type the password');

$car = $this->choose('What is your favourite car?', ['Ferrari', 'Lamborghini', 'Maserati'], 1);

$food = $this->anticipate('What is your favourite food?', ['Pizza', 'Pasta', 'Lasagna'], 'Mozzarella');

$colors = $this->choice('What are your favourite colors (defaults to blue and red)?', ['Blue', 'Red', 'Green'], '0,1');

protected function handle()
{
    $this->output->write('Message'); // plain text

    $this->output->writeInfo('Message');  // green text

    $this->output->writeError('Message');  // red text

    $this->output->writeComment('Message');  // yellow text
}

protected function handle()
{
    $output = $this->getOutputInterface(); // $output is instance of Symfony\Component\Console\Output\OutputInterface
}

use Yarak\Console\ConsoleKernel;
use App\Console\Commands\ExampleCommand;
use App\Comsone\Commands\YourCustomCommand;

class Kernel extends ConsoleKernel
{
    /**
     * Your custom Yarak commands.
     *
     * @var array
     */
    protected $commands = [
        ExampleCommand::class,
        YourCustomCommand::class
    ];
}


public static function call($command, array $arguments = [], \Phalcon\DiInterface $di = null)

use Yarak\Yarak;

Yarak::call('migrate:rollback', [
    '--steps'    => 2,
]);

use Yarak\Yarak;

Yarak::call('migrate:rollback', [
    '--steps'    => 2,
], $di);

php yarak

php yarak db:generate

php yarak make:seeder SeederName

php yarak db:seed SeederName

php yarak migrate:refresh --seed --class=SeederName

php yarak make:migration increase_user_email_column_size

php yarak migrate

php yarak migrate:rollback

php yarak migrate:rollback --steps=2

// with shortcut
php yarak migrate:rollback -s=2

php yarak migrate:reset

php yarak migrate:refresh

php yarak console:generate

php yarak make:command CommandName

php yarak namespace:name argument --opt

php yarak namespace:name argument --opt=one --opt=two --opt=three