This commit is contained in:
Marcel Pociot
2020-06-02 16:51:36 +02:00
parent f058ce8c5c
commit 6a47264be9
34 changed files with 661 additions and 9 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Client\Support;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
class TokenNodeVisitor extends NodeVisitorAbstract
{
/** @var string */
protected $token;
public function __construct(string $token)
{
$this->token = $token;
}
public function enterNode(Node $node)
{
if ($node instanceof Node\Expr\ArrayItem && $node->key->value === 'auth_token') {
$node->value->value = $this->token;
return $node;
}
return null;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Commands;
use Illuminate\Console\Command;
class PublishCommand extends Command
{
protected $signature = 'publish';
protected $description = 'Publish the expose configuration file';
public function handle()
{
$configFile = implode(DIRECTORY_SEPARATOR, [
$_SERVER['HOME'],
'.expose',
'config.php'
]);
if (file_exists($configFile)) {
$this->error('Expose configuration file already exists at '.$configFile);
return;
}
file_put_contents($configFile, file_get_contents(base_path('config/expose.php')));
$this->info('Published expose configuration file to: ' . $configFile);
}
}

View File

@@ -8,7 +8,7 @@ use React\EventLoop\LoopInterface;
class ServeCommand extends Command
{
protected $signature = 'serve {host=0.0.0.0} {hostname=localhost} {--validateAuthTokens}';
protected $signature = 'serve {hostname=localhost} {host=0.0.0.0} {--validateAuthTokens}';
protected $description = 'Start the shaft server';

View File

@@ -28,6 +28,6 @@ class ShareCurrentWorkingDirectoryCommand extends ShareCommand
return $valetConfig->tld;
}
return 'test';
return config('expose.default_tld', 'test');
}
}

View File

@@ -2,7 +2,14 @@
namespace App\Commands;
use App\Client\Support\TokenNodeVisitor;
use Illuminate\Console\Command;
use PhpParser\Lexer\Emulative;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\Parser\Php7;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
class StoreAuthenticationTokenCommand extends Command
{
@@ -17,17 +24,20 @@ class StoreAuthenticationTokenCommand extends Command
if (!is_null($this->argument('token'))) {
$this->info('Setting the expose authentication token to "' . $this->argument('token') . '"');
$config['auth_token'] = $this->argument('token');
$configFile = implode(DIRECTORY_SEPARATOR, [
$_SERVER['HOME'],
'.expose',
'config.php'
]);
@mkdir(dirname($configFile), 0777, true);
if (! file_exists($configFile)) {
@mkdir(dirname($configFile), 0777, true);
$updatedConfigFile = $this->modifyConfigurationFile(base_path('config/expose.php'), $this->argument('token'));
} else {
$updatedConfigFile = $this->modifyConfigurationFile($configFile, $this->argument('token'));
}
file_put_contents($configFile, '<?php return ' . var_export($config, true) . ';');
file_put_contents($configFile, $updatedConfigFile);
return;
}
@@ -37,4 +47,32 @@ class StoreAuthenticationTokenCommand extends Command
$this->info('Current authentication token: ' . $token);
}
}
protected function modifyConfigurationFile(string $configFile, string $token)
{
$lexer = new Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startTokenPos', 'endTokenPos',
],
]);
$parser = new Php7($lexer);
$oldStmts = $parser->parse(file_get_contents($configFile));
$oldTokens = $lexer->getTokens();
$nodeTraverser = new NodeTraverser;
$nodeTraverser->addVisitor(new CloningVisitor());
$newStmts = $nodeTraverser->traverse($oldStmts);
$nodeTraverser = new NodeTraverser;
$nodeTraverser->addVisitor(new TokenNodeVisitor($token));
$newStmts = $nodeTraverser->traverse($newStmts);
$prettyPrinter = new Standard();
return $prettyPrinter->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
}
}