mirror of
https://github.com/flarum/framework.git
synced 2025-03-02 21:37:24 +08:00
Add extension generator command.
This commit is contained in:
parent
ef73b0cabb
commit
a41d02f030
@ -13,6 +13,7 @@ class ConsoleServiceProvider extends ServiceProvider
|
||||
{
|
||||
$this->commands('Flarum\Console\InstallCommand');
|
||||
$this->commands('Flarum\Console\SeedCommand');
|
||||
$this->commands('Flarum\Console\GenerateExtensionCommand');
|
||||
}
|
||||
|
||||
public function register()
|
||||
|
127
framework/core/src/Console/GenerateExtensionCommand.php
Normal file
127
framework/core/src/Console/GenerateExtensionCommand.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php namespace Flarum\Console;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
|
||||
class GenerateExtensionCommand extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'flarum:extension';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generate a Flarum extension skeleton.';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
do {
|
||||
$name = $this->ask('Extension name (<vendor>-<name>):');
|
||||
} while (! preg_match('/^([a-z0-9]+)-([a-z0-9-]+)$/i', $name, $match));
|
||||
|
||||
list(, $vendor, $package) = $match;
|
||||
|
||||
do {
|
||||
$title = $this->ask('Title:');
|
||||
} while (! $title);
|
||||
|
||||
$description = $this->ask('Description:');
|
||||
|
||||
$authorName = $this->ask('Author name:');
|
||||
|
||||
$authorEmail = $this->ask('Author email:');
|
||||
|
||||
$license = $this->ask('License:');
|
||||
|
||||
$this->info('Generating extension skeleton for "'.$name.'"...');
|
||||
|
||||
$dir = public_path().'/extensions/'.$name;
|
||||
|
||||
$replacements = [
|
||||
'{{namespace}}' => ucfirst($vendor).'\\'.ucfirst($package),
|
||||
'{{escapedNamespace}}' => ucfirst($vendor).'\\\\'.ucfirst($package),
|
||||
'{{classPrefix}}' => ucfirst($package),
|
||||
'{{name}}' => $name
|
||||
];
|
||||
|
||||
$this->copyStub($dir, $replacements);
|
||||
|
||||
rename($dir.'/src/ServiceProvider.php', $dir.'/src/'.ucfirst($package).'ServiceProvider.php');
|
||||
|
||||
$manifest = [
|
||||
'name' => $name,
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
'tags' => [],
|
||||
'version' => '0.1.0',
|
||||
'author' => [
|
||||
'name' => $authorName,
|
||||
'email' => $authorEmail
|
||||
],
|
||||
'license' => $license,
|
||||
'require' => [
|
||||
'php' => '>=5.4.0',
|
||||
'flarum' => '>0.1.0'
|
||||
]
|
||||
];
|
||||
|
||||
file_put_contents($dir.'/flarum.json', json_encode($manifest, JSON_PRETTY_PRINT));
|
||||
|
||||
passthru("cd $dir; composer install; cd js; npm install; gulp");
|
||||
|
||||
$this->info('Extension "'.$name.'" generated!');
|
||||
}
|
||||
|
||||
protected function copyStub($destination, $replacements = [])
|
||||
{
|
||||
$this->recursiveCopy(__DIR__.'/../../stubs/extension', $destination, $replacements);
|
||||
}
|
||||
|
||||
protected function recursiveCopy($src, $dst, $replacements = [])
|
||||
{
|
||||
$dir = opendir($src);
|
||||
@mkdir($dst);
|
||||
|
||||
while (($file = readdir($dir)) !== false) {
|
||||
if ($file != '.' && $file != '..') {
|
||||
if (is_dir($src.'/'.$file)) {
|
||||
$this->recursiveCopy($src.'/'.$file, $dst.'/'.$file, $replacements);
|
||||
}
|
||||
else {
|
||||
$contents = file_get_contents($src.'/'.$file);
|
||||
$contents = str_replace(array_keys($replacements), array_values($replacements), $contents);
|
||||
|
||||
file_put_contents($dst.'/'.$file, $contents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closedir($dir);
|
||||
}
|
||||
}
|
9
framework/core/stubs/extension/bootstrap.php
Normal file
9
framework/core/stubs/extension/bootstrap.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
// Require the extension's composer autoload file. This will enable all of our
|
||||
// classes in the src directory to be autoloaded.
|
||||
require __DIR__.'/vendor/autoload.php';
|
||||
|
||||
// Register our service provider with the Flarum application. In here we can
|
||||
// register bindings and execute code when the application boots.
|
||||
return $this->app->register('{{namespace}}\{{classPrefix}}ServiceProvider');
|
11
framework/core/stubs/extension/composer.json
Normal file
11
framework/core/stubs/extension/composer.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"require": {
|
||||
"php": ">=5.4.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"{{escapedNamespace}}\\": "src/"
|
||||
}
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
}
|
3
framework/core/stubs/extension/js/.gitignore
vendored
Normal file
3
framework/core/stubs/extension/js/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
bower_components
|
||||
node_modules
|
||||
dist
|
5
framework/core/stubs/extension/js/Gulpfile.js
Normal file
5
framework/core/stubs/extension/js/Gulpfile.js
Normal file
@ -0,0 +1,5 @@
|
||||
var gulp = require('flarum-gulp');
|
||||
|
||||
gulp({
|
||||
modulePrefix: '{{name}}'
|
||||
});
|
7
framework/core/stubs/extension/js/bootstrap.js
vendored
Normal file
7
framework/core/stubs/extension/js/bootstrap.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
import { extend, override } from 'flarum/extension-utils';
|
||||
|
||||
app.initializers.add('{{name}}', function() {
|
||||
|
||||
// @todo
|
||||
|
||||
});
|
7
framework/core/stubs/extension/js/package.json
Normal file
7
framework/core/stubs/extension/js/package.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"gulp": "^3.8.11",
|
||||
"flarum-gulp": "git+https://github.com/flarum/gulp.git"
|
||||
}
|
||||
}
|
0
framework/core/stubs/extension/less/extension.less
Normal file
0
framework/core/stubs/extension/less/extension.less
Normal file
32
framework/core/stubs/extension/src/ServiceProvider.php
Normal file
32
framework/core/stubs/extension/src/ServiceProvider.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php namespace {{namespace}};
|
||||
|
||||
use Flarum\Support\ServiceProvider;
|
||||
use Flarum\Extend\ForumAssets;
|
||||
|
||||
class {{classPrefix}}ServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->extend(
|
||||
new ForumAssets([
|
||||
__DIR__.'/../js/dist/extension.js',
|
||||
__DIR__.'/../less/extension.less'
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user