Laravel 空白项目..这么多文件!能不能减少一点?

问题描述 投票:0回答:2

我上次使用 Laravel 已经是很久以前的事了,我决定重新开始使用它。

现在来自 CodeIgniter,它在当时是一个强大的框架,我很高兴将项目上传到网站,因为包含框架文件的“系统”文件夹仅包含 121 个文件。

然而,基于 Composer 的解决方案的问题是,一个小项目可能会变得巨大,比当时非常大规模的 CodeIgniter 项目大得多。当有时只使用一种方法时,所有依赖项都有测试文件夹、文档和大量模块。

当我使用官方文档中的说明创建一个空的 Laravel 项目并看到包含超过 8,000 个文件的“vendor”文件夹时,我倒吸一口冷气! (不计算文件夹)而且它什么也没做。顺便说一下,这是使用

--prefer-dist
标志时的情况。我知道
--no-dev
参数,它仍然有 5,000 多个文件。我的观点是,不可能使用所有这些文件,尤其是在使用分发渠道时。

所以我的问题是,是否有一种方法可以拥有一个更具选择性的空 Laravel 项目,因为服务器通常具有有限的索引节点,并且每个项目的 8,000 个文件 + 文件夹会让你很快达到限制(如果不能的话,上传会永远持续下去)在您的服务器上安装 Composer)。

php laravel laravel-5 composer-php
2个回答
2
投票

Composer 可以删除无关文件。

在项目的

composer.json
中,使用
archive
和/或
exclude-files-from-classmaps
配置值指定不需要的文件,然后使用 Composer 的
archive
命令创建 zip。上传 zip 并在服务器上展开,或在本地展开并传输现在较小的包。

$ cat composer.json
...
{
    "archive": {
        "exclude": ["!vendor", "/test/*", "/*.jpg" ]
    }
}
$ php composer.phar archive --format=zip --file=<filename-without-extension>

那些与

archive
匹配的文件根本不会出现在您的 zip 中。那些与
exclude-files-from-classmaps
匹配的文件将出现在文件系统中,但对自动加载器不可见。


1
投票

几天前我遇到了同样的情况,所以我创建了控制台命令来删除vendor目录

中未使用的文件

步骤:1

php artisan make:command CleanVendorFolderCommand

步骤:2

复制当前代码并将 int 粘贴到命令类中

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use FilesystemIterator;

class CleanVendorFolderCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'clean:vendor {--o : Verbose Output} {--dry : Runs in dry mode without deleting files.}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Cleans up useless files from  vendor folder.';

    protected $patterns = 
            [
                'test',
                'tests',
                '.github',
                'README',
                'CHANGELOG',
                'FAQ',
                'CONTRIBUTING',
                'HISTORY',
                'UPGRADING',
                'UPGRADE',
                'demo',
                'example',
                'examples',
                '.doc',
                'readme',
                'changelog',
                'composer',
                '.git',
                '.gitignore',
                '*.md',
                '.*.yml',
                '*.yml',
                '*.txt',
                '*.dist',
                'LICENSE',
                'AUTHORS',
                '.eslintrc',
                'ChangeLog',
                '.gitignore',
                '.editorconfig',
                '*.xml',                
                '.npmignore',
                '.jshintrc',
                'Makefile',
                '.keep',

            ];
    /**
     * List of File and Folders Patters Going To Be Excluded
     *
     * @return void
     */
    protected $excluded = 
            [
                /**List of  Folders*/
                'src',
                /**List of  Files*/
                '*.php',
                '*.stub',
                '*.js',
                '*.json',
            ];
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct() 
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle() 
    {
        $patterns = array_diff($this->patterns, $this->excluded);

        $directories = $this->expandTree(base_path('vendor'));

        $isDry = $this->option('dry');

        foreach ($directories as $directory) 
        {
            foreach ($patterns as $pattern) 
            {
                $casePattern = preg_replace_callback('/([a-z])/i', [$this, 'prepareWord'], $pattern);
                $files = glob($directory . '/' . $casePattern, GLOB_BRACE);
                if (!$files) 
                {
                    continue;
                }

                $files = array_diff($files, $this->excluded);
                foreach ($this->excluded as $excluded) 
                {
                    $key = $this->arrayFind($excluded, $files);

                    if ($key !== false) 
                    {
                        $this->warn('SKIPPED: ' . $files[$key]);
                        unset($files[$key]);
                    }
                }
                foreach ($files as $file) 
                {
                    if (is_dir($file)) 
                    {
                        $this->warn('DELETING DIR: ' . $file);
                        if (!$isDry) 
                        {
                            $this->delTree($file);
                        }
                    } else 
                    {
                        $this->warn('DELETING FILE: ' . $file);
                        if (!$isDry) 
                        {
                            @unlink($file);
                        }
                    }
                }
            }
        }
        $this->warn('Folder Cleanup Done!');
    }
    /**
     * Recursively traverses the directory tree
     *
     * @param  string $dir
     * @return array
     */
    protected function expandTree($dir) 
    {
        $directories = [];
        $files = array_diff(scandir($dir), ['.', '..']);
        foreach ($files as $file) 
        {
            $directory = $dir . '/' . $file;
            if (is_dir($directory)) 
            {
                $directories[] = $directory;
                $directories = array_merge($directories, $this->expandTree($directory));
            }
        }
        return $directories;
    }
    /**
     * Recursively deletes the directory
     *
     * @param  string $dir
     * @return bool
     */
    protected function delTree($dir) {
        if (!file_exists($dir) || !is_dir($dir)) 
        {
            return false;
        }

        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($iterator as $filename => $fileInfo) 
        {
            if ($fileInfo->isDir()) 
            {
                @rmdir($filename);
            } else {
                @unlink($filename);
            }
        }
        @rmdir($dir);
    }
    /**
     * Prepare word
     *
     * @param  string $matches
     * @return string
     */
    protected function prepareWord($matches) 
    {
        return '[' . strtolower($matches[1]) . strtoupper($matches[1]) . ']';
    }
    protected function arrayFind($needle, array $haystack) 
    {
        foreach ($haystack as $key => $value) 
        {
            if (false !== stripos($value, $needle)) 
            {
                return $key;
            }
        }
        return false;
    }
    protected function out($message) 
    {
        if ($this->option('o') || $this->option('dry')) 
        {
            echo $message . PHP_EOL;
        }
    }
}

已测试

OS Name
Microsoft Windows 10 专业版

Version
10.0.16299 内部版本 16299

Processor
Intel(R) Core(TM) i3-7100U CPU @ 2.40GHz,2400 Mhz,2 个核心,4 个逻辑处理器

现在是测试部分

供应商文件夹大小之前

Size
57.0 MB(5,98,29,604 字节)

Size on disk
75.2 MB(7,88,80,768 字节)

Contains
12,455 个文件,2,294 个文件夹

现在运行命令

php artisan clean:vendor

运行命令后供应商文件夹的大小

Size
47.0 MB(4,93,51,781 字节)

Size on disk
59.7 MB(6,26,76,992 字节)

Contains
8,431 个文件,1,570 个文件夹

希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.