Implementation of .gitignore class rules based on togos/phpgitignore for parsing and applying .gitignore class rules
1. There is a folder at this stage, which contains a configuration file similar to .gitignore: .themeignore, the content is as follows
node_modules/*
npm-debug.log
yarn-error.log
*.bak
*.swp
*.map
*.LICENSE.txt
.DS_Store
css/*
fixtures/*
images/*
js/*
mix/*
modules/*/images/*
modules/*/js/*
.browserslistrc
.env
.env.*
.themeignore
babel.config.json
jsconfig.json
package.json
postcss.config.js
README.md
tailwind.config.js
webpack.mix.js
2. You need to ignore some files based on this file configuration, and only include the required files. For example: filter out node_modules/* . as shown in Figure 1
3. Install togos/gitignore based on composer
PS E:\wwwroot\object> composer require togos/gitignore
Info from https://repo.packagist.org: #StandWithUkraine
Using version ^1.1 for togos/gitignore
./composer.json has been updated
Running composer update togos/gitignore
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
- Locking demo/facebook-conversions-api (2.2.0)
- Locking togos/gitignore (1.1.1)
Writing lock file
4. The final reference /vendor/togos/gitignore/src/test/php/togos/gitignore/filefindertest.php is implemented as follows
use TOGoS_GitIgnore_FileFinder;
use TOGoS_GitIgnore_Ruleset;
protected $results;
public function addResult($f, $result) {
$this->results[$f] = $result;
}
/* 测试忽略文件 */
$rules1Content = file_get_contents('E:/wwwroot/object/resources/views/theme/.themeignore');
$finder = new TOGoS_GitIgnore_FileFinder(array(
'ruleset' => TOGoS_GitIgnore_Ruleset::loadFromString($rules1Content),
'invertRulesetResult' => false,
'defaultResult' => false,
'includeDirectories' => false,
'callback' => array($this,'addResult')
));
$this->results = array();
$finder->findFiles('E:/wwwroot/object/resources/views/theme');
var_dump($this->results);
exit;
5. Print $this-> results, the result is as follows, in line with expectations. The value corresponding to the file path is true, which means that the file corresponding to this path is ignored. as shown in Figure 2
array(685) {
[".browserslistrc"]=>
bool(true)
[".env"]=>
bool(true)
[".env.development"]=>
bool(true)
// ...
["tailwind.config.js"]=>
bool(true)
["theme.json"]=>
bool(false)
["webpack.mix.js"]=>
bool(true)
}
6. You can generate a new and clean directory based on the old directory. You only need to judge based on $this-> results when traversing the directory.

