PHP dynamic Page-level DocBlocks

2019-07-26 01:33发布

问题:

I was wondering if there is a way to interact with the Page-level DocBlocks. My question is more specifically about wordpress plugin development, but this question has arised also in a non-wordpress environments.

The reason is mainly the possibility to easily change VERSIONS and names throughout a large project with maybe a constant definition - but that will reflect also in the docblock..

The following example Docblock is from a wordpress plugin I write -

/*
Plugin Name: o99 Auxilary Functions v0.4.7
Plugin URI: http://www.myurl.com
Description: some simple description that nobody reads.
Version: 0.4.7
Author: my cool name
Author URI: http://www.ok-alsouri.com
*/

Is there a way to transform it into :

$ver = '0.4.7';
$uri = 'http://www.myurl.com';
$desc = 'some simple description that nobody reads.';
$mcn = 'my cool name';
etc.. 
etc..

    /*
    Plugin Name: o99 Auxilary Functions ($ver)
    Plugin URI: ($uri)
    Description: ($desc)
    Version: ($ver)
    Author: ($mcn)
    Author URI: ($$uri)
    */

obviously for echo to work I would need to break the docblock itself, and I can not WRITE the docblock directly into it´s own file .

In shorts : can I "generate" a docblock with php itself somehow (I would think that the answer is - "no" for the page itself.. But maybe I am wrong and someone has some neat hack :-) )

Is that even possible ?

回答1:

You could do:

$ver = '0.4.7';
$uri = 'http://www.myurl.com';
$desc = 'some simple description that nobody reads.';
$mcn = 'my cool name';
etc.. 
etc..

$docblock = <<<TEMPLATE
/*
Plugin Name: o99 Auxilary Functions ($ver)
Plugin URI: $uri
Description: $desc
Version: $ver
Author: $mcn
Author URI: $uri
*/
TEMPLATE;

$file_data = $docblock;
$file_data .= file_get_contents('yourplugin.php');
file_put_contents('yourplugin.php', $file_data);