mckenziearts/laravel-command

一个简单的Laravel包,用于提供新的artisan命令

安装: 960

依赖者: 0

建议者: 0

安全: 0

星星: 32

关注者: 3

分支: 2

开放问题: 0

类型:laravel-package

1.0.1 2018-05-29 07:09 UTC

This package is auto-updated.

Last update: 2024-09-24 05:40:17 UTC


README

Latest Stable Version License Build Status Total Downloads

一个简单的包,可以快速生成Laravel模板化的Repository、Helper和Observer文件。

安装

通过Composer

$ composer require mckenziearts/laravel-command --dev

对于Laravel 5.5 - 你就完成了。

对于Laravel 5.4或5.3,你只想在local开发中使用这些命令,因此你不想更新production的providers数组在config/app.php中。相反,你可以在app/Providers/AppServiceProvider.php中添加provider,如下所示

public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('Mckenziearts\LaravelCommand\LaravelCommandServiceProvider');
    }
}

使用方法

默认情况下,Laravel不允许生成观察者,甚至不允许将仓库的概念视为Symfony。要生成其元素,你可以使用以下命令

打开控制台并输入此命令以生成新的仓库

php artisan make:repository {Entity}

生成的文件看起来像这样

namespace App\Repositories;

use App\Models\Entity;

class EntityRepository
{
    /**
     * @var Entity
     */
    private $model;

    /**
     * EntityRepository constructor.
     * @param Entity $model
     */
    public function __construct(Entity $model)
    {
        $this->model = $model;
    }

    /**
     * Return a new instance of Entity Model
     *
     * @return Entity
     */
    public function newInstance()
    {
        return $this->model->newInstance();
    }
}

默认情况下,Repository在默认的应用程序命名空间App\Models中加载模型。如果你的模型在其他命名空间中,则必须在仓库中更改use以避免错误,如下所示

use MODELS\NAMESPACE\Entity;

这与执行观察者的操作相同,它也在命名空间App\Models中加载模型。要生成观察者,必须执行以下命令

php artisan make:observer {Entity}

生成的文件看起来像这样

namespace App\Observers;

use App\Models\Entity;

class EntityObserver
{
    /**
     * Trigger Before Create a Entity
     *
     * @param Entity $model
     */
    public function creating(Entity $model){}

    /**
     * Trigger after create a Entity
     *
     * @param Entity $model
     */
    public function created(Entity $model){}

    /**
     * Trigger before update a Entity
     *
     * @param Entity $model
     */
    public function updating(Entity $model){}

    ...
}
  • Helper文件
$ php artisan make:helper {Entity}

生成的文件看起来像这样

namespace App\Helpers;

class EntityHelper
{

}

如果你需要更好地分发你的代码,你可以创建一个helper来将逻辑放入控制器中,以减轻其负担。

以下是我经常在我项目中使用的helper的示例

namespace App\Helpers;

use Intervention\Image\Facades\Image;

class MediaHelper
{
    /**
     * @protected
     *
     * @var string $dir, the file uploaded path
     */
    protected static $dir = 'uploads';

    /**
     * @return string
     */
    public static function getUploadsFolder()
    {
        return self::$dir;
    }

    /**
     * Return the size of an image
     *
     * @param string $file
     * @param string $folder
     * @return array $width and $height of the file give in parameter
     */
    public static function getFileSizes(string $file, string $folder = null)
    {
        if ($folder) {
            list($width, $height, $type, $attr) = getimagesize(public_path(self::$dir.'/'. $folder .'/'.$file));
        }
        list($width, $height, $type, $attr) = getimagesize(public_path(self::$dir.'/'.$file));

        return [
            'width'     => $width,
            'height'    => $height
        ];
    }

    /**
     * resize, To rezise and image
     *
     * @param string    $file      file to rezise
     * @param int       $width     width of the file
     * @param int       $height    height of the file
     * @param string    $filepath  path to save file
     */
    public static function resize($file, $width, $height, $filepath)
    {
        Image::make($file)->resize($width, $height)->save($filepath);
    }

    /**
     * getImageWeight
     *
     * @param $octets
     * @return string
     */
    public static function getImageWeight($octets) {
        $resultat = $octets;
        for ($i = 0; $i < 8 && $resultat >= 1024; $i++) {
            $resultat = $resultat / 1024;
        }
        if ($i > 0) {
            return preg_replace('/,00$/', '', number_format($resultat, 2, ',', ''))
                . ' ' . substr('KMGTPEZY', $i-1, 1) . 'o';
        } else {
            return $resultat . ' o';
        }
    }

}

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

许可证

MIT许可证(MIT)。