拉拉维尔空白项目. 这么多文件!可以以某种方式减少吗?



我上次使用Laravel是很久以前的事了,我决定回到它。

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

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

当使用官方文档中的说明创建一个空的Laravel项目并看到包含8,000多个文件的"供应商"文件夹时,我喘了口气!!(不包括文件夹)而且它还没有做任何事情..顺便说一下,那是在使用--prefer-dist标志的时候。我知道--no-dev论点,它仍然有 5,000+ 个文件。我的观点是,不可能使用所有这些文件,尤其是在使用分发渠道时。

所以我的问题是是否有办法拥有一个更具选择性的空 Laravel 项目,因为服务器通常具有有限的 Inode,每个项目的 8,000 个文件 + 文件夹会让您很快达到限制(如果您无法在服务器上安装 composer,上传需要很长时间)。

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

php artisan make:command CleanVendorFolderCommand

步数:2

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

<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
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 NameMicrosoft 视窗 10 专业版

Version10.0.16299 内部版本 16299

Processor英特尔® 酷睿(TM) i3-7100U CPU @ 2.40GHz, 2400 MHz, 2 核, 4 逻辑处理器

现在是测试部分

供应商文件夹大小之前

Size57.0 兆字节 (5,98,29,604 字节)

Size on disk75.2 兆字节 (7,88,80,768 字节)

Contains12,455 个文件,2,294 个文件夹

现在运行命令

php artisan clean:vendor

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

Size47.0 兆字节 (4,93,51,781 字节)

Size on disk59.7 兆字节 (6,26,76,992 字节)

Contains8,431 个文件,1,570 个文件夹

希望对你有帮助

最新更新