Giter VIP home page Giter VIP logo

mezon-cli's Introduction

Mezon CLI Tool

Open Collective Packagist Twitter

Installation

composer global require mezon/cli

Command line pattern

Here is the most common pattern:

mezon <verb> <entity> [<options>]

Here <verb> is:

  • create
  • help
  • version

And <entity> can be:

  • htaccess (default .htaccess will be created);
  • fs (default file structure will be created);
  • application (default Application.php file with default class will be created).
  • project (default .env will be created with settings for the project).

mezon-cli's People

Contributors

alexdodonov avatar yazgoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

lolix-notepad

mezon-cli's Issues

Create htaccess file

mezon create htaccess

This command must create .htaccess file with the following content:

# use mod_rewrite for pretty URL support
RewriteEngine on

RewriteRule ^(.*).html$ $1.html [L]
RewriteRule ^(.*).png$ $1.png [L]

RewriteRule ^cron/(.*)$ cron/$1 [L]
RewriteRule ^include/(.*)$ include/$1 [L]
RewriteRule ^Res/(.*)$ Res/$1 [L]
RewriteRule ^vendor/(.*)$ vendor/$1 [L]

RewriteRule ^data/files/(.*)$ data/files/$1 [L]

RewriteRule ^Res/Images/(.*)$ Res/Images/$1 [L]
RewriteRule ^([a-z0-9A-Z_\/\.\-\@%\ :,]+)/?(.*)$ index.php?r=$1&%{QUERY_STRING} [L]
RewriteRule ^/?(.*)$ index.php?r=index&%{QUERY_STRING} [L]

Create application class

Create default application with command

mezon create application

File must be ./%project-name%/Application.php

Creating table

New entity creation feature need to be added:

create table

This command acts like described below:

  1. asks user for DB table name %table name% for wich this model will be created
  2. asks for amount of fields in this DB
  3. asks names and data types for each field. Ask for each field - should it be created as primary index and stops asking if the user selected one field to be primary index
  4. creates directory %project name%/Models/SQL
  5. creates file %project name%/Models/SQL/%entity name%.sql and outputs SQL script for creating this table
  6. creates file %project name%/Models/SQL/%entity name%.sql.meta and outputs creating table description in it (table name, fields and types of that fields)

Create model

Add new command

create model

This command acts like this:

  1. Searches all files *.sql.meta (see #13)
  2. Outputs list of tables found in *.sql.meta files
  3. User selects on of the outputted table
  4. Then CLI
  • creates file %project name%/Models/%Table name%Model.php
  • fills the created file with the content:
<?php
namespace %Project name%\Models;

use Mezon\Functional\Fetcher;
use Mezon\PdoCrud\ConnectionTrait;

class %Table name%Model
{
    use ConnectionTrait;

    /**
     * Loaded data
     *
     * @var object
     */
    private $data = null;

    /**
     * Constructor
     *
     * @param object $data
     *            record data
     */
    public function __construct(object $data = null)
    {
        $this->data = $data;
    }

    /**
     * Method returns record's raw data
     *
     * @return object record's raw data
     */
    public function getData(): object
    {
        return $this->data;
    }

    /**
     * Method asserts that data was loaded
     */
    private function assertDataWasLoaded(): void
    {
        if ($this->data=== null) {
            throw (new \Exception('Data was not loaded', - 1));
        }
    }

    /**
     * Method deletes record
     *
     * @return int id of the deleted record
     */
    public function delete(): int
    {
        $this->assertDataWasLoaded();

        static::deleteById($answerId = Fetcher::getField($this->answer, 'id'));

        $this->answer = null;

        return $answerId;
    }

    /**
     * Method returns field value
     *
     * @param string $fieldName
     *            field name
     * @return mixed field value
     */
    private function getField(string $fieldName)
    {
        $this->assertDataWasLoaded();

        return Fetcher::getField($this->data, $fieldName);
    }

    /**
     * Method deletes single record
     *
     * @param int $id
     *            answers's id
     */
    public static function deleteById(int $id): void
    {
        static::getConnection()->prepare('DELETE FROM %table name% WHERE id = :id');
        static::getConnection()->bindParameter(':%primary index field name%', $id, \PDO::PARAM_INT);
        static::getConnection()->execute();
    }
}

Note that %Table name% is a table name wich starts from big letter. For example we have table 'user' and %Table name% will be 'User'.

Create default folder structure

mezon create fs

Directory structure must be:

./%project name%/Actions
./%project name%/ListBuilderAdapters
./%project name%/Logic
./%project name%/Middleware
./%project name%/Models
./%project name%/Presenters
./%project name%/Repositories
./%project name%/Tables
./%project name%/Views
./Conf
./Include/Js
./Include/Php
./Res/Images
./Testing/Selenium
./Testing/Unit

Before creating directories %project name% must be prompted

In the root of the each directory empty file index.html must be created to prevent direct access to this directory.

Refactoring

These two entities should be refactored:

Mezon/Cli/Entities/Application
Mezon/Cli/Entities/Htaccess

Because they have huge amount of diplicationg code - we check if the file exists, prompt for entity name, handle input.

This code need to be extracted into common using method.

Create method Tool::loadVerbs

  1. This method must accept $path to the folder with verbs or accept '' and scan default directory with verbs
  2. Allow multyple calls:
Tool::loadVerbs('path/to/dir1');
Tool::loadVerbs('path/to/dir2');
// here we can use verbs from dir 1 and dir2
Tool::run();
  1. Unit-tests required
  2. composer psalm must show no errors
  3. composer infection must show 100% msi score

It will be the first step to let users create their on CLI scripts for their projects.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.