This plugin collects and creates pages that contain content to the official Github documentation README of dependencies in a composer file.
This plugin requires that the generator.php
has to be run from the PHP command line in a terminal environment.
After modifying the generator.php
file, which can be found in folder <GRAV Root folder>/user/plugins/gitdoc
, you're good to go.
define( "PAGE_FOLDER", PAGE_ROOT . "/05.slim/04.composer-package-documentation" ); /* (1) */
define( "COMPOSER_JSON", dirname( __FILE__ ) . "/composer/composer.json" ); /* (2) */
/user/pages/
under which the pages will be generated.composer
under the root of your GRAV folder. Now you do from the root folder of the plugin in a terminal:
php generator.php
Because of caching in GRAV, it can take a while before the pages show up the first time.
<?php
define( "GRAV_ROOT", dirname( __FILE__, 4 ) );
define( "PAGE_ROOT", GRAV_ROOT . "/user/pages" );
define( "PAGE_FOLDER", PAGE_ROOT . "/05.slim/04.composer-package-documentation" );
define( "COMPOSER_JSON", dirname( __FILE__ ) . "/composer/composer.json" );
$template = "
---
title: '%s'
---
%s(0)
";
echo "\n--- Grav Root ---\n";
var_dump( GRAV_ROOT );
echo "\n--- Page Root ---\n";
var_dump( PAGE_ROOT );
echo "\n--- Page Folder ---\n";
var_dump( PAGE_FOLDER );
echo "\n--- Composer File ---\n";
var_dump( COMPOSER_JSON );
/* Step 1. read the composer.json */
echo "\n--- Read composer.json ---\n";
if ( !file_exists( COMPOSER_JSON ) ) {
echo "\nComposer file not found\n";
echo "Exit!\n";
exit;
}
$composerData = file_get_contents( COMPOSER_JSON );
$composer = json_decode( $composerData );
if ( !isset( $composer->require ) ) {
echo "No requirements found\n";
echo "Exit!\n";
exit;
}
/* Step 2. clear the root folder contents */
echo "\n--- Deleting folder contents ---\n";
delete_files( PAGE_FOLDER );
echo "\n (status) Folder contents deleted\n";
/* Step 3. iterate the composer.json */
echo "\n--- Iterate composer.json ---\n";
$counter = 1;
foreach ( $composer->require as $package => $version ) {
echo "(status) Processing " . $package . "\n";
$dirname = PAGE_FOLDER . "/" . str_pad( $counter, 2, "0", STR_PAD_LEFT ) . "." . str_replace( "/", "_", $package );
$filename = "default.en.md";
$content = sprintf($template, $package, $package);
if ( is_dir( $dirname ) === false ) {
mkdir( $dirname, 777, true );
}
$file = fopen( $dirname . "/" . $filename, "w" );
fwrite( $file, $content );
fclose( $file );
$counter ++;
}
/* Step 4. ready */
echo "\n----------------------------------------------------------------------------\n";
echo "(finised) All packages have been processed, upload it to your GRAV CMS pages\n";
echo "----------------------------------------------------------------------------\n";
function delete_files( $target ) {
if ( is_dir( $target ) ) {
$files = glob( $target . "*", GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
foreach ( $files as $file ) {
delete_files( $file );
}
rmdir( $target );
} elseif ( is_file( $target ) ) {
unlink( $target );
}
}