Skip to content

Instantly share code, notes, and snippets.

@rxu
Created October 9, 2015 15:45
Show Gist options
  • Save rxu/af652c7ffbb79c05e9dc to your computer and use it in GitHub Desktop.
Save rxu/af652c7ffbb79c05e9dc to your computer and use it in GitHub Desktop.
phpBB 3.1 cache ext autoloaders
/**
* Load the autoloaders added by the extensions.
*
* @param string $phpbb_root_path Path to the phpbb root directory.
*/
function phpbb_load_extensions_autoloaders($phpbb_root_path, $phpEx)
{
$cache_filename = $phpbb_root_path . 'cache/autoload.' . $phpEx;
if (!file_exists($cache_filename))
{
$extensions_autoloaders_cache_file_data = phpbb_create_extensions_autoloaders_cache_file_data($phpbb_root_path);
// Assume it will work ... if nothing goes wrong below
$written = true;
if (!($fp = @fopen($cache_filename, 'w')))
{
// Something went wrong ... so let's try another method
$written = false;
}
if (!(@fwrite($fp, $extensions_autoloaders_cache_file_data)))
{
// Something went wrong ... so let's try another method
$written = false;
}
@fclose($fp);
if ($written)
{
// For safety, set chmod() to CHMOD_READ
phpbb_chmod($cache_filename, CHMOD_READ);
}
}
require($cache_filename);
}
/**
* Creates the output to be stored in a phpBB cache/autoload.php file
*
* @return string The output to write to the file
*/
function phpbb_create_extensions_autoloaders_cache_file_data($phpbb_root_path)
{
$extensions_autoloaders_data = "<?php\n";
$extensions_autoloaders_data .= "// phpBB auto-generated extensions autoloaders cache file\n// Do not change anything in this file!\n\n";
$extensions_autoloaders_data .= "if (!defined('IN_PHPBB'))\n{\n";
$extensions_autoloaders_data .= " exit;\n}\n";
$iterator = new \RecursiveIteratorIterator(
new \phpbb\recursive_dot_prefix_filter_iterator(
new \RecursiveDirectoryIterator(
$phpbb_root_path . 'ext/',
\FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS
)
),
\RecursiveIteratorIterator::SELF_FIRST
);
$iterator->setMaxDepth(2);
foreach ($iterator as $file_info)
{
if ($file_info->getFilename() === 'vendor' && $iterator->getDepth() === 2)
{
$filename = $file_info->getRealPath() . DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($filename))
{
$extensions_autoloaders_data .= "\nrequire('$filename');";
}
}
}
$extensions_autoloaders_data .= "\n";
return $extensions_autoloaders_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment