Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make looking for badges directory recursive. #24

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
34 changes: 30 additions & 4 deletions src/GamifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
use QCod\Gamify\Console\MakePointCommand;
use QCod\Gamify\Events\ReputationChanged;

use \RecursiveDirectoryIterator;
use \RecursiveIteratorIterator;
use \RecursiveRegexIterator;
use \RegexIterator;

class GamifyServiceProvider extends ServiceProvider
{
/**
Expand Down Expand Up @@ -77,10 +82,31 @@ protected function getBadges()

$badges = [];

foreach (glob(app_path('/Gamify/Badges/') . '*.php') as $file) {
if (is_file($file)) {
$badges[] = app($badgeRootNamespace . '\\' . pathinfo($file, PATHINFO_FILENAME));
}
// Get the first folder for the app. For the vast majority of all projects this is "App"
$rootFolder = substr($badgeRootNamespace, 0, strpos($badgeRootNamespace, '\\'));

// Create recursive searching classes
$directory = new RecursiveDirectoryIterator(app_path('Gamify/Badges/'));
$iterator = new RecursiveIteratorIterator($directory);
$files = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

// loop through each file found
foreach ($files as $file) {

// grab the directory for the file
$fileDirectory = pathinfo($file[0], PATHINFO_DIRNAME);

//remove full server path and prepend the rootfolder
$fileDirectory = $rootFolder.str_ireplace(app_path(), '', $fileDirectory);

// convert the forward slashes to backslashes
$fileDirectory = str_ireplace('/', '\\', $fileDirectory);

// get the file name
$fileName = pathinfo($file[0], PATHINFO_FILENAME);

//append namespace file path to the badges array to return
$badges[] = $fileDirectory."\\".$fileName;
}

return collect($badges);
Expand Down