There is no problem not worth solving, and no technology not worth learning!

Implementation of .gitignore class rules based on togos/phpgitignore for parsing and applying .gitignore class rules

打印 $this->results,结果如下,符合预期。文件路径所对应的值为 true,则表示此路径对应的文件为忽略

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

需要基于此文件配置将一些文件忽略掉,仅纳入需要的文件。比如说:过滤掉 node_modules/*
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

打印 $this->results,结果如下,符合预期。文件路径所对应的值为 true,则表示此路径对应的文件为忽略
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.

 

Need long-term technical maintenance or remote troubleshooting?

I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.

If your project is facing any of the following issues, we can start with a small troubleshooting task first:

  • ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
  • ✅ Go / Gin backend APIs that need troubleshooting or optimization
  • ✅ Slow, broken, or unstable WordPress websites
  • ✅ Nginx / MySQL / Redis / Linux server issues
  • ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
  • ✅ Long-term remote technical support or part-time maintenance

More details: About Me & Collaboration

WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.