Skip to content

Instantly share code, notes, and snippets.

@webmozart
Last active July 22, 2018 13:39
Show Gist options
  • Save webmozart/8323103 to your computer and use it in GitHub Desktop.
Save webmozart/8323103 to your computer and use it in GitHub Desktop.
Automatically replacing variables in PHP files when doing "git add"
*.php filter=subvars
%%PackageName%%=My Project
%%CopyrightHolder%%=Me <[email protected]>
  1. Place .gitattributes in your home directory

  2. Add .gitvars to your project directory and customize it

  3. Execute:

     git config --global core.attributesfile ~/.gitattributes
     git config --global filter.subvars.clean 'php -r "echo file_exists(\".gitvars\") ? strtr(file_get_contents(\"php://stdin\"), parse_ini_file(\".gitvars\")) : file_get_contents(\"php://stdin\");"'
    
  4. Set up your IDE to create new PHP files similar to template.php

  5. Enjoy ;)

ATTENTION: One caveat of a filter on *.php files is that checkouts of branches with many PHP files become quite slow. I had to disable the filter again for this reason.

<?php
/*
* This file is part of the %%PackageName%% package.
*
* (c) %%CopyrightHolder%%
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
...
@stof
Copy link

stof commented Jan 15, 2014

It might be better to write a small plugin for your IDE to allow injecting the variables into your template on class creation based on the ini file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment