-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1433 from mosen/feature/package_discovery
Package discovery and more testing
- Loading branch information
Showing
20 changed files
with
442 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Composer\Package\CompletePackage; | ||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class Packages extends ResourceCollection | ||
{ | ||
/** | ||
* Transform the resource collection into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return $this->collection->map(function (CompletePackage $package) { | ||
return [ | ||
'name' => $package->getName(), | ||
'prettyName' => $package->getPrettyName(), | ||
'version' => $package->getVersion(), | ||
'prettyVersion' => $package->getPrettyVersion(), | ||
'description' => $package->getDescription(), | ||
]; | ||
})->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Composer\Composer; | ||
use Composer\Factory; | ||
use Composer\IO\BufferIO; | ||
use Composer\Package\Link; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* The packages model provides an index of installed packages via composer which provide modules. | ||
* | ||
* It is registered into the DI container by PackageServiceProvider | ||
*/ | ||
class Packages | ||
{ | ||
public function __construct() | ||
{ | ||
$io = new BufferIO(); | ||
|
||
// When invoked from the web application, Composer will assume public/ contains a composer.json | ||
// So, we need to hint the actual location of composer.json | ||
$composer = Factory::create($io); | ||
|
||
$this->composer = $composer; | ||
$this->io = $io; | ||
} | ||
|
||
/** | ||
* Get a list of all Composer packages installed. | ||
* | ||
* @return array|null | ||
*/ | ||
public function index() | ||
{ | ||
$mr = $this->composer->getPackage(); | ||
|
||
if ($mr === null) { | ||
Log::alert('composer returned a null package, maybe you have removed composer.json/composer.lock or it is not accessible by the webserver'); | ||
Log::info($this->io->getOutput()); | ||
|
||
return null; | ||
} | ||
|
||
$packages = []; | ||
|
||
$localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); | ||
|
||
foreach ($this->composer->getPackage()->getRequires() as $required) { | ||
if ($required instanceof Link) | ||
{ | ||
$p = $localRepo->findPackage($required->getTarget(), $required->getConstraint()); | ||
if ($p === null) continue; | ||
$packages[] = $p->getPrettyString(); | ||
} | ||
} | ||
|
||
return $packages; | ||
} | ||
|
||
/** | ||
* Get a list of Composer packages installed which are MunkiReport PHP modules, either: | ||
* | ||
* - Name starts `munkireport/` | ||
* - composer.json extras contains `munkireport` key | ||
* | ||
* @return array|null | ||
*/ | ||
public function modules() | ||
{ | ||
$mr = $this->composer->getPackage(); | ||
|
||
if ($mr === null) { | ||
Log::alert('composer returned a null package, maybe you have removed composer.json/composer.lock or it is not accessible by the webserver'); | ||
Log::info($this->io->getOutput()); | ||
|
||
return null; | ||
} | ||
|
||
$packages = []; | ||
$localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); | ||
foreach ($this->composer->getPackage()->getRequires() as $required) { | ||
if ($required instanceof Link) { | ||
$p = $localRepo->findPackage($required->getTarget(), $required->getConstraint()); | ||
if ($p === null) continue; | ||
if (Arr::has($p->getExtra(), 'munkireport')) { | ||
$packages[] = $p; | ||
} else { | ||
if (!Str::startsWith($p->getName(), 'munkireport/')) continue; | ||
$packages[] = $p; | ||
} | ||
} | ||
} | ||
|
||
return $packages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use App\Packages; | ||
use Composer\Composer; | ||
use Composer\Factory; | ||
use Composer\IO\BufferIO; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class PackageServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Bootstrap services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$config = realpath(__DIR__ . '/../../composer.json'); | ||
Log::alert($config); | ||
|
||
$this->app->singleton(Packages::class, function ($app) use ($config) { | ||
return new Packages; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.