2018-07-21 17:26:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Glossary:
|
|
|
|
* test classes - Classes that ClassFinder will attempt to find.
|
|
|
|
* test app - a directory structure containing a composer.json. This directory structure is intended to simulate an application
|
|
|
|
* that can autoload classes with psr-4.
|
|
|
|
*
|
|
|
|
* Due to the nature of the this component, the ClassFinder class must have access to the classes in the test app.
|
|
|
|
* For this reason, we copy in ClassFinder and ensure that it can be autoloaded with the test classes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Find test apps
|
|
|
|
|
|
|
|
function findTestApps($rootDir) {
|
|
|
|
$testAppPaths = scandir(__DIR__);
|
|
|
|
|
|
|
|
$testAppPaths = array_filter($testAppPaths, function($path) {
|
|
|
|
return substr($path, 0, 3) === 'app';
|
|
|
|
});
|
|
|
|
|
|
|
|
$testAppPaths = array_map(function($path) {
|
2018-07-21 23:36:21 +02:00
|
|
|
return realpath(__DIR__ . '/' . $path);
|
2018-07-21 17:26:01 +02:00
|
|
|
}, $testAppPaths);
|
|
|
|
|
|
|
|
return $testAppPaths;
|
|
|
|
}
|
|
|
|
|
2018-08-04 17:20:34 +02:00
|
|
|
// https://stackoverflow.com/a/3349792/3000068
|
|
|
|
function deleteDir($dirPath) {
|
|
|
|
if (! is_dir($dirPath)) {
|
|
|
|
throw new InvalidArgumentException("$dirPath must be a directory");
|
2018-07-21 17:26:01 +02:00
|
|
|
}
|
2018-08-04 17:20:34 +02:00
|
|
|
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
|
|
|
|
$dirPath .= '/';
|
|
|
|
}
|
|
|
|
$files = glob($dirPath . '*', GLOB_MARK);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (is_dir($file)) {
|
|
|
|
deleteDir($file);
|
|
|
|
} else {
|
|
|
|
unlink($file);
|
2018-07-21 17:26:01 +02:00
|
|
|
}
|
2018-08-04 17:20:34 +02:00
|
|
|
}
|
|
|
|
rmdir($dirPath);
|
|
|
|
}
|
2018-07-21 17:26:01 +02:00
|
|
|
|
2018-08-04 17:20:34 +02:00
|
|
|
// https://stackoverflow.com/a/2050909/3000068
|
|
|
|
function recurse_copy($src,$dst) {
|
|
|
|
$dir = opendir($src);
|
2018-08-04 18:03:21 +02:00
|
|
|
@mkdir($dst, 0777, true);
|
2018-08-04 17:20:34 +02:00
|
|
|
while(false !== ( $file = readdir($dir)) ) {
|
|
|
|
if (( $file != '.' ) && ( $file != '..' )) {
|
|
|
|
if ( is_dir($src . '/' . $file) ) {
|
|
|
|
recurse_copy($src . '/' . $file,$dst . '/' . $file);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
copy($src . '/' . $file,$dst . '/' . $file);
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 17:26:01 +02:00
|
|
|
}
|
2018-08-04 17:20:34 +02:00
|
|
|
closedir($dir);
|
2018-07-21 17:26:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function copyInCurrentClasses($testApp) {
|
2018-08-04 17:20:34 +02:00
|
|
|
$classFinderSource = realpath(__DIR__ . '/../src');
|
2018-08-04 18:03:21 +02:00
|
|
|
$classFinderPath = $testApp . '/vendor/haydenpierce/class-finder/src';
|
2018-07-21 17:26:01 +02:00
|
|
|
$classFinderPath = str_replace('\\', '/', $classFinderPath);
|
|
|
|
|
2018-08-04 17:20:34 +02:00
|
|
|
if (is_dir($classFinderPath)) {
|
|
|
|
deleteDir($classFinderPath);
|
2018-07-21 17:26:01 +02:00
|
|
|
}
|
|
|
|
|
2018-08-04 17:20:34 +02:00
|
|
|
recurse_copy($classFinderSource, $classFinderPath);
|
|
|
|
|
|
|
|
$composerSource = realpath(__DIR__ . '/../composer.json');
|
2018-08-04 18:03:21 +02:00
|
|
|
$composerPath = $testApp . '/vendor/haydenpierce/class-finder/composer.json';
|
2018-08-04 17:20:34 +02:00
|
|
|
copy($composerSource, $composerPath);
|
2018-07-21 17:26:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$testApps = findTestApps(__DIR__);
|
|
|
|
|
|
|
|
foreach($testApps as $testApp) {
|
2018-07-21 23:36:21 +02:00
|
|
|
|
|
|
|
$autoloaderPath = $testApp . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
$autoloaderExists = false;
|
|
|
|
if (!file_exists($autoloaderPath)) {
|
|
|
|
echo "No autoloader detected.\n";
|
|
|
|
echo "Running composer install for $testApp...\n";
|
|
|
|
|
|
|
|
// TODO: Programically run composer install?
|
|
|
|
// https://stackoverflow.com/a/45831624/3000068
|
|
|
|
// I ran into a problem getting composer to run via shell_exec() - Stack Overflow suggests it could be xdebug related.
|
|
|
|
// I couldn't figure out how to set an environment variable before running a command on Windows.
|
|
|
|
// This should be capable of Windows and Unix systems.
|
|
|
|
echo "FAILED.\n";
|
|
|
|
|
|
|
|
echo "************\n";
|
|
|
|
echo "* SOLUTION *\n";
|
|
|
|
echo "************\n";
|
|
|
|
echo "You will need to run the following command to get this up and running:\n";
|
|
|
|
echo "composer install --working-dir=\"$testApp\"\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2018-07-21 17:26:01 +02:00
|
|
|
copyInCurrentClasses($testApp);
|
|
|
|
}
|