Skip to content
This repository was archived by the owner on Aug 13, 2020. It is now read-only.

Commit aa5f6dd

Browse files
committed
Now everything is based on project-cached-code-generator-from-composer.json-data-base to make better support for extras parsing in dependencies and project
1 parent ae0f7fa commit aa5f6dd

9 files changed

+182
-251
lines changed

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"require": {
66
"composer-plugin-api": "^1.1",
77
"php": ">=5.5",
8-
"symfony/console": "^4.1"
8+
"symfony/console": "^4.1",
9+
"imponeer/project-cached-code-generator-from-composer.json-data-base": "^0.1.1"
910
},
1011
"license": "MIT",
1112
"authors": [

Diff for: cache/.gitignore renamed to generated/.gitignore

File renamed without changes.

Diff for: src/CommandProvider.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ class CommandProvider implements CommandProviderCapability {
1616
* @return array
1717
*/
1818
public function getCommands() {
19-
return ProxyCommand::produce(
20-
DataCache::getInstance()->read()
21-
);
19+
$file = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'generated' . DIRECTORY_SEPARATOR . 'data.php';
20+
$commands = [];
21+
if (file_exists($file)) {
22+
$commands = include($file);
23+
}
24+
return $commands;
2225
}
2326
}

Diff for: src/DataCache.php

-109
This file was deleted.

Diff for: src/DefaultDumpWriter.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Imponeer\ComposerCustomCommands;
4+
5+
use Imponeer\ProjectCachedCodeGeneratorFromComposerJSONDataBase\DumpWriterInterface;
6+
7+
class DefaultDumpWriter implements DumpWriterInterface
8+
{
9+
10+
/**
11+
* Filename where to write generated container data
12+
*
13+
* @var string
14+
*/
15+
protected $filename;
16+
17+
/**
18+
* Generated source
19+
*
20+
* @var string
21+
*/
22+
protected $source;
23+
24+
/**
25+
* Command classes
26+
*
27+
* @var string[]
28+
*/
29+
protected $commands = [];
30+
31+
/**
32+
* Constructor
33+
*
34+
* @param string $filename Filename to write
35+
*/
36+
public function __construct(string $filename)
37+
{
38+
$this->filename = $filename;
39+
$this->source = sprintf(
40+
'<?php%1$srequire_once(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . "autoload.php");%1$s$_SERVER[\'HTTP_HOST\'] = null;%1$s',
41+
PHP_EOL
42+
);
43+
}
44+
45+
/**
46+
* Write existing service configuration to file
47+
*
48+
* @return bool
49+
*/
50+
public function writeToFile(): bool
51+
{
52+
$ret = $this->source . PHP_EOL . 'return [' . PHP_EOL;
53+
foreach ($this->commands as $command) {
54+
$ret .= str_repeat(' ', 4) . ' new ' . $command . '(),' . PHP_EOL;
55+
}
56+
$ret .= '];';
57+
58+
return (bool)file_put_contents($this->filename, $ret, LOCK_EX);
59+
}
60+
61+
/**
62+
* Add classes of commands that should be registered
63+
*
64+
* @param string[] $commands Commands to add
65+
*/
66+
public function addCommands(array $commands): void
67+
{
68+
$this->commands = array_merge($this->commands, $commands);
69+
}
70+
71+
/**
72+
* Add bootscript
73+
*
74+
* @param string $bootScript Bootscript file that should be executed for commands
75+
*/
76+
public function addBootScript(string $bootScript): void
77+
{
78+
$this->source .= sprintf(
79+
'file_exists(%1$s) && include_once(%1$s);%2$s',
80+
json_encode($bootScript),
81+
PHP_EOL
82+
);
83+
}
84+
}

Diff for: src/DumpWriterFactory.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Imponeer\ComposerCustomCommands;
4+
5+
use Imponeer\ProjectCachedCodeGeneratorFromComposerJSONDataBase\DumpWriterFactoryInterface;
6+
use Imponeer\ProjectCachedCodeGeneratorFromComposerJSONDataBase\DumpWriterInterface;
7+
8+
class DumpWriterFactory implements DumpWriterFactoryInterface
9+
{
10+
/**
11+
* Place where in extras all config variables are stored
12+
*/
13+
const CONFIG_NAMESPACE = 'custom-commands';
14+
15+
/**
16+
* DumpWriter instance
17+
*
18+
* @var DefaultDumpWriter
19+
*/
20+
protected $instance;
21+
22+
/**
23+
* DumpWriterFactory constructor.
24+
*/
25+
public function __construct()
26+
{
27+
$this->instance = new DefaultDumpWriter(
28+
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'generated' . DIRECTORY_SEPARATOR . 'data.php'
29+
);
30+
}
31+
32+
/**
33+
* Add config
34+
*
35+
* @param array[] $extra Extra part of config
36+
*/
37+
public function addConfig(array $extra): void
38+
{
39+
if (!isset($extra[self::CONFIG_NAMESPACE])) {
40+
return;
41+
}
42+
43+
$data = $extra[self::CONFIG_NAMESPACE];
44+
if (isset($data['boot'])) {
45+
$this->instance->addBootScript((string)$data['boot']);
46+
}
47+
if (isset($data['commands'])) {
48+
$this->instance->addCommands((array)$data['commands']);
49+
}
50+
}
51+
52+
/**
53+
* Create dump writer instance
54+
*
55+
* @return null|DumpWriterInterface
56+
*/
57+
public function create(): ?DumpWriterInterface
58+
{
59+
return $this->instance;
60+
}
61+
}

Diff for: src/Plugin.php

+29-38
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,66 @@
22

33
namespace Imponeer\ComposerCustomCommands;
44

5-
use Composer\Composer;
6-
use Composer\EventDispatcher\EventSubscriberInterface;
7-
use Composer\IO\IOInterface;
85
use Composer\Plugin\Capability\CommandProvider;
9-
use Composer\Plugin\Capable;
10-
use Composer\Plugin\PluginInterface;
11-
use Composer\Script\Event;
12-
use Composer\Script\ScriptEvents;
136
use Imponeer\ComposerCustomCommands\CommandProvider as LocalCommandProvider;
7+
use Imponeer\ProjectCachedCodeGeneratorFromComposerJSONDataBase\ComposerPlugin;
148

159
/**
1610
* Defines plugin
1711
*
1812
* @package Imponeer\ComposerCustomCommand
1913
*/
20-
class Plugin implements PluginInterface, Capable, EventSubscriberInterface
14+
class Plugin extends ComposerPlugin
2115
{
2216

2317
/**
24-
* Place where in extras all config variables are stored
25-
*/
26-
const CONFIG_NAMESPACE = 'custom-commands';
27-
28-
/**
29-
* Gets all subscribed events for plugin
18+
* Gets capabilities of object
3019
*
3120
* @return array
3221
*/
33-
public static function getSubscribedEvents() {
22+
public function getCapabilities() {
3423
return array(
35-
ScriptEvents::POST_AUTOLOAD_DUMP => array('onPostAutoloadDump', 0)
24+
CommandProvider::class => LocalCommandProvider::class
3625
);
3726
}
3827

3928
/**
40-
* Method executed when activating plugin
29+
* Gets dump writer factory class
4130
*
42-
* @param Composer $composer Composer instance
43-
* @param IOInterface $io IO interface
31+
* @return string
4432
*/
45-
public function activate(Composer $composer, IOInterface $io) {
46-
33+
protected function getDumpWriterFactoryClass(): string
34+
{
35+
return DumpWriterFactory::class;
4736
}
4837

4938
/**
50-
* Gets capabilities of object
39+
* Get message when can't create dump writer instance
5140
*
52-
* @return array
41+
* @return string
5342
*/
54-
public function getCapabilities() {
55-
return array(
56-
CommandProvider::class => LocalCommandProvider::class
57-
);
43+
protected function getCannotCreateDumpWriterInstanceMessage(): string
44+
{
45+
return 'No commands definitions found in your composer.json project definitions';
5846
}
5947

6048
/**
61-
* Using post autodump event to create cached version of commands
49+
* Get message when updating cache
6250
*
63-
* @param Event $event
64-
*
65-
* @throws CommandsConfigIsNotArrayException
51+
* @return string
6652
*/
67-
public function onPostAutoloadDump(Event $event) {
68-
$composer = $event->getComposer();
69-
$extra = $composer->getPackage()->getExtra();
70-
71-
$event->getIO()->write('<info>Updating commands cache</info>');
72-
$commands = (isset($extra[self::CONFIG_NAMESPACE]) && isset($extra[self::CONFIG_NAMESPACE]['commands']))?$extra[self::CONFIG_NAMESPACE]['commands']:array();
53+
protected function getUpdatingCacheMessage(): string
54+
{
55+
return 'Updating commands cache';
56+
}
7357

74-
DataCache::getInstance()->write($commands);
58+
/**
59+
* Get message when can't write cache file
60+
*
61+
* @return string
62+
*/
63+
protected function getFailToWriteDumpMessage(): string
64+
{
65+
return 'Failed to update commands cache';
7566
}
7667
}

0 commit comments

Comments
 (0)