Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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