Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from ipublikuj/nette30
Browse files Browse the repository at this point in the history
Task: Better nette 3 integration
  • Loading branch information
akadlec authored Nov 15, 2019
2 parents 6ee3838 + fb7c0a9 commit 2da01ef
Show file tree
Hide file tree
Showing 19 changed files with 569 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/IPub/Packages/Commands/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function configure() : void
/**
* @param Input\InputInterface $input
* @param Output\OutputInterface $output
*
*
* @return void
*/
protected function execute(Input\InputInterface $input, Output\OutputInterface $output) : void
Expand Down
2 changes: 1 addition & 1 deletion src/IPub/Packages/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function configure() : void
/**
* @param Input\InputInterface $input
* @param Output\OutputInterface $output
*
*
* @return void
*/
protected function execute(Input\InputInterface $input, Output\OutputInterface $output) : void
Expand Down
2 changes: 1 addition & 1 deletion src/IPub/Packages/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $

foreach ($packages as $package) {
$output->writeln(sprintf(
'<info>%'. $maxLength .'s</info> | status: <comment>%-12s</comment> | version: <comment>%s</comment>',
'<info>%' . $maxLength . 's</info> | status: <comment>%-12s</comment> | version: <comment>%s</comment>',
$package->getName(),
$this->packageManager->getStatus($package),
$this->packageManager->getVersion($package)
Expand Down
14 changes: 7 additions & 7 deletions src/IPub/Packages/Commands/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
$output->writeln(sprintf('<info>%s : %s</info>', $action, $name));
}
}
/*
foreach ($this->packageManager->disableAbsent() as $item) {
foreach ($item as $name => $action) {
$output->writeln(sprintf('<info>%s : %s</info>', $action, $name));
}
}
*/
/*
foreach ($this->packageManager->disableAbsent() as $item) {
foreach ($item as $name => $action) {
$output->writeln(sprintf('<info>%s : %s</info>', $action, $name));
}
}
*/
} catch (Exceptions\InvalidArgumentException $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/IPub/Packages/Commands/UninstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function configure() : void
/**
* @param Input\InputInterface $input
* @param Output\OutputInterface $output
*
*
* @return void
*/
protected function execute(Input\InputInterface $input, Output\OutputInterface $output) : void
Expand Down
73 changes: 62 additions & 11 deletions src/IPub/Packages/DI/PackagesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use IPub\Packages;
use IPub\Packages\Commands;
use IPub\Packages\Events;
use IPub\Packages\Helpers;
use IPub\Packages\Loaders;
use IPub\Packages\Repository;
Expand All @@ -41,19 +42,20 @@ final class PackagesExtension extends DI\CompilerExtension
* @var array
*/
private $defaults = [
'path' => NULL, // Paths where to search for packages
'dirs' => [ // Define path to folders
'path' => NULL, // Paths where to search for packages
'dirs' => [ // Define path to folders
'configDir' => '%appDir%/config', // Path where is stored app configuration
'vendorDir' => '%appDir%/../vendor', // Path to composer vendor folder
'tempDir' => '%tempDir%', // Path to temporary folder
],
'configFile' => 'config.neon', // Filename with enabled packages extensions
'loader' => [
'configFile' => 'config.neon', // Filename with enabled packages extensions
'loader' => [
'packageFiles' => [
'package.php',
],
],
'sources' => [],
'sources' => [],
'symfonyEvents' => FALSE,
];

/**
Expand All @@ -63,12 +65,12 @@ public function loadConfiguration() : void
{
// Get container builder
$builder = $this->getContainerBuilder();

// Merge extension default config
$this->setConfig(DI\Config\Helpers::merge($this->config, DI\Helpers::expand($this->defaults, $builder->parameters)));

// Get extension configuration
$configuration = $this->getConfig();
/** @var array $configuration */
if (method_exists($this, 'validateConfig')) {
$configuration = $this->validateConfig($this->defaults);
} else {
$configuration = $this->getConfig($this->defaults);
}

/**
* Load packages configuration
Expand Down Expand Up @@ -150,13 +152,62 @@ public function beforeCompile() : void

// Get container builder
$builder = $this->getContainerBuilder();
/** @var array $configuration */
if (method_exists($this, 'validateConfig')) {
$configuration = $this->validateConfig($this->defaults);
} else {
$configuration = $this->getConfig($this->defaults);
}

// Get packages manager
$manager = $builder->getDefinition($this->prefix('manager'));

foreach ($builder->findByType(Packages\Scripts\IScript::class) as $serviceDefinition) {
$manager->addSetup('addScript', [$serviceDefinition->getType(), $serviceDefinition]);
}

if ($configuration['symfonyEvents'] === TRUE) {
$dispatcher = $builder->getDefinition($builder->getByType(EventDispatcher\EventDispatcherInterface::class));

$packagesManager = $builder->getDefinition($builder->getByType(Packages\PackagesManager::class));
assert($packagesManager instanceof DI\ServiceDefinition);

$packagesManager->addSetup('?->onEnable[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\EnableEvent::class),
]);
$packagesManager->addSetup('?->onDisable[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\DisableEvent::class),
]);
$packagesManager->addSetup('?->onUpgrade[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\UpgradeEvent::class),
]);
$packagesManager->addSetup('?->onInstall[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\InstallEvent::class),
]);
$packagesManager->addSetup('?->onUninstall[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\UninstallEvent::class),
]);
$packagesManager->addSetup('?->onRegister[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\RegisterEvent::class),
]);
$packagesManager->addSetup('?->onUnregister[] = function() {?->dispatch(new ?(...func_get_args()));}', [
'@self',
$dispatcher,
new Nette\PhpGenerator\PhpLiteral(Events\UnregisterEvent::class),
]);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/IPub/Packages/Entities/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public function getPath() : string
public function compare(IPackage $package, string $operator = '==') : bool
{
return strtolower($this->getName()) === strtolower($package->getName()) &&
version_compare(strtolower($this->getVersion()), strtolower($package->getVersion()), $operator);
version_compare(strtolower($this->getVersion()), strtolower($package->getVersion()), $operator);
}

/**
Expand Down
69 changes: 69 additions & 0 deletions src/IPub/Packages/Events/DisableEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* DisableEvent.php
*
* @copyright More in license.md
* @license https://www.ipublikuj.eu
* @author Adam Kadlec <[email protected]>
* @package iPublikuj:Packages!
* @subpackage Events
* @since 1.0.0
*
* @date 15.11.19
*/

namespace IPub\Packages\Events;

use Symfony\Contracts\EventDispatcher;

use IPub\Packages;
use IPub\Packages\Entities;

/**
* Package disabled event
*
* @package iPublikuj:Packages!
* @subpackage Events
*
* @author Adam Kadlec <[email protected]>
*/
final class DisableEvent extends EventDispatcher\Event
{
/**
* @var Packages\PackagesManager
*/
private $manager;

/**
* @var Entities\IPackage
*/
private $package;

/**
* @param Packages\PackagesManager $manager
* @param Entities\IPackage $package
*/
public function __construct(
Packages\PackagesManager $manager,
Entities\IPackage $package
) {
$this->manager = $manager;
$this->package = $package;
}

/**
* @return Packages\PackagesManager
*/
public function getManager() : Packages\PackagesManager
{
return $this->manager;
}

/**
* @return Entities\IPackage
*/
public function getPackage() : Entities\IPackage
{
return $this->package;
}
}
69 changes: 69 additions & 0 deletions src/IPub/Packages/Events/EnableEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* EnableEvent.php
*
* @copyright More in license.md
* @license https://www.ipublikuj.eu
* @author Adam Kadlec <[email protected]>
* @package iPublikuj:Packages!
* @subpackage Events
* @since 1.0.0
*
* @date 15.11.19
*/

namespace IPub\Packages\Events;

use Symfony\Contracts\EventDispatcher;

use IPub\Packages;
use IPub\Packages\Entities;

/**
* Package enabled event
*
* @package iPublikuj:Packages!
* @subpackage Events
*
* @author Adam Kadlec <[email protected]>
*/
final class EnableEvent extends EventDispatcher\Event
{
/**
* @var Packages\PackagesManager
*/
private $manager;

/**
* @var Entities\IPackage
*/
private $package;

/**
* @param Packages\PackagesManager $manager
* @param Entities\IPackage $package
*/
public function __construct(
Packages\PackagesManager $manager,
Entities\IPackage $package
) {
$this->manager = $manager;
$this->package = $package;
}

/**
* @return Packages\PackagesManager
*/
public function getManager() : Packages\PackagesManager
{
return $this->manager;
}

/**
* @return Entities\IPackage
*/
public function getPackage() : Entities\IPackage
{
return $this->package;
}
}
69 changes: 69 additions & 0 deletions src/IPub/Packages/Events/InstallEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* InstallEvent.php
*
* @copyright More in license.md
* @license https://www.ipublikuj.eu
* @author Adam Kadlec <[email protected]>
* @package iPublikuj:Packages!
* @subpackage Events
* @since 1.0.0
*
* @date 15.11.19
*/

namespace IPub\Packages\Events;

use Symfony\Contracts\EventDispatcher;

use IPub\Packages;
use IPub\Packages\Entities;

/**
* Package installed event
*
* @package iPublikuj:Packages!
* @subpackage Events
*
* @author Adam Kadlec <[email protected]>
*/
final class InstallEvent extends EventDispatcher\Event
{
/**
* @var Packages\PackagesManager
*/
private $manager;

/**
* @var Entities\IPackage
*/
private $package;

/**
* @param Packages\PackagesManager $manager
* @param Entities\IPackage $package
*/
public function __construct(
Packages\PackagesManager $manager,
Entities\IPackage $package
) {
$this->manager = $manager;
$this->package = $package;
}

/**
* @return Packages\PackagesManager
*/
public function getManager() : Packages\PackagesManager
{
return $this->manager;
}

/**
* @return Entities\IPackage
*/
public function getPackage() : Entities\IPackage
{
return $this->package;
}
}
Loading

0 comments on commit 2da01ef

Please sign in to comment.