Permalink
Browse files

Extract the logic so we can reuse it on a controller

  • Loading branch information...
1 parent ded938b commit f0517b5a52803d9eaa00a44e5b504295bdb1c8f2 @nickvergessen nickvergessen committed May 29, 2015
Showing with 385 additions and 227 deletions.
  1. +2 −2 config/console.yml
  2. +15 −0 config/services.yml
  3. +87 −0 config/skeletons.yml
  4. +13 −225 console/create.php
  5. +203 −0 helper/packager.php
  6. +2 −0 language/en/common.php
  7. +63 −0 skeleton.php
View
4 config/console.yml
@@ -3,7 +3,7 @@ services:
class: phpbb\skeleton\console\create
arguments:
- @user
- - @path_helper
- - %core.root_path%
+ - @phpbb.skeleton.helper.packager
+ - @phpbb.skeleton.helper.validator
tags:
- { name: console.command }
View
15 config/services.yml
@@ -1,2 +1,17 @@
imports:
- { resource: console.yml }
+ - { resource: skeletons.yml }
+
+services:
+ phpbb.skeleton.helper.packager:
+ class: phpbb\skeleton\helper\packager
+ arguments:
+ - @user
+ - @path_helper
+ - @phpbb.skeleton.collection
+ - %core.root_path%
+
+ phpbb.skeleton.helper.validator:
+ class: phpbb\skeleton\helper\validator
+ arguments:
+ - @user
View
87 config/skeletons.yml
@@ -0,0 +1,87 @@
+services:
+ phpbb.skeleton.collection:
+ class: phpbb\di\service_collection
+ arguments:
+ - @service_container
+ tags:
+ - { name: service_collection, tag: phpbb.skeleton.ext.skeleton }
+
+ phpbb.skeleton.ext.skeleton.phplistener:
+ class: phpbb\skeleton\skeleton
+ arguments:
+ - 'phplistener'
+ - true
+ - []
+ - ['config/services.yml', 'event/main_listener.php']
+ tags:
+ - { name: phpbb.skeleton.ext.skeleton }
+
+ phpbb.skeleton.ext.skeleton.htmllistener:
+ class: phpbb\skeleton\skeleton
+ arguments:
+ - 'htmllistener'
+ - true
+ - []
+ - ['styles/prosilver/template/event/overall_header_navigation_prepend.html']
+ tags:
+ - { name: phpbb.skeleton.ext.skeleton }
+
+ phpbb.skeleton.ext.skeleton.acp:
+ class: phpbb\skeleton\skeleton
+ arguments:
+ - 'acp'
+ - true
+ - []
+ - ['acp/main_info.php', 'acp/main_module.php', 'adm/style/demo_body.html', 'language/en/info_acp_demo.php']
+ tags:
+ - { name: phpbb.skeleton.ext.skeleton }
+
+ phpbb.skeleton.ext.skeleton.migration:
+ class: phpbb\skeleton\skeleton
+ arguments:
+ - 'migration'
+ - true
+ - []
+ - ['migrations/release_1_0_0.php', 'migrations/release_1_0_1.php']
+ tags:
+ - { name: phpbb.skeleton.ext.skeleton }
+
+ phpbb.skeleton.ext.skeleton.service:
+ class: phpbb\skeleton\skeleton
+ arguments:
+ - 'service'
+ - true
+ - []
+ - ['service.php', 'config/services.yml', 'config/parameters.yml']
+ tags:
+ - { name: phpbb.skeleton.ext.skeleton }
+
+ phpbb.skeleton.ext.skeleton.controller:
+ class: phpbb\skeleton\skeleton
+ arguments:
+ - 'controller'
+ - true
+ - []
+ - ['config/routing.yml', 'config/services.yml', 'event/main_listener.php', 'language/en/common.php', 'controller/main.php', 'styles/prosilver/template/demo_body.html']
+ tags:
+ - { name: phpbb.skeleton.ext.skeleton }
+
+ phpbb.skeleton.ext.skeleton.tests:
+ class: phpbb\skeleton\skeleton
+ arguments:
+ - 'tests'
+ - true
+ - []
+ - ['tests/controller/main_test.php', 'tests/dbal/fixtures/config.xml', 'tests/dbal/simple_test.php', 'tests/functional/demo_test.php', 'tests/mock/controller_helper.php', 'tests/mock/template.php', 'tests/mock/user.php', 'phpunit.xml.dist']
+ tags:
+ - { name: phpbb.skeleton.ext.skeleton }
+
+ phpbb.skeleton.ext.skeleton.travis:
+ class: phpbb\skeleton\skeleton
+ arguments:
+ - 'travis'
+ - true
+ - ['tests']
+ - ['travis/prepare-phpbb.sh', '.travis.yml']
+ tags:
+ - { name: phpbb.skeleton.ext.skeleton }
View
238 console/create.php
@@ -13,150 +13,37 @@
namespace phpbb\skeleton\console;
-use phpbb\config\config;
use phpbb\console\command\command;
-use phpbb\path_helper;
+use phpbb\skeleton\helper\packager;
use phpbb\skeleton\helper\validator;
-use phpbb\template\context;
-use phpbb\template\twig\twig;
use phpbb\user;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Filesystem\Filesystem;
class create extends command
{
protected $data = array();
- /** @var \phpbb\skeleton\helper\validator */
+ /** @var validator */
protected $validator;
- protected function get_composer_dialog_values()
- {
- return array(
- 'author' => array(
- 'author_name' => null,
- 'author_email' => null,
- 'author_homepage' => null,
- 'author_role' => null,
- ),
- 'extension' => array(
- 'vendor_name' => null,
- 'extension_display_name' => null,
- 'extension_name' => null,
- 'extension_description' => null,
- 'extension_version' => '1.0.0-dev',
- 'extension_homepage' => null,
- 'extension_time' => date('Y-m-d'),
- ),
- 'requirements' => array(
- 'php_version' => '>=5.3.3',
- 'phpbb_version_min' => '>=3.1.4',
- 'phpbb_version_max' => '<3.2.0@dev',
- )
- );
- }
-
- protected function get_component_dialog_values()
- {
- return array(
- 'phplistener' => array(
- 'default' => true,
- 'dependencies' => array(),
- 'files' => array(
- 'config/services.yml',
- 'event/main_listener.php',
- ),
- ),
- 'htmllistener' => array(
- 'default' => true,
- 'dependencies' => array(),
- 'files' => array(
- 'styles/prosilver/template/event/overall_header_navigation_prepend.html',
- ),
- ),
- 'acp' => array(
- 'default' => true,
- 'dependencies' => array(),
- 'files' => array(
- 'acp/main_info.php',
- 'acp/main_module.php',
- 'adm/style/demo_body.html',
- 'language/en/info_acp_demo.php',
- ),
- ),
- 'migration' => array(
- 'default' => true,
- 'dependencies' => array(),
- 'files' => array(
- 'migrations/release_1_0_0.php',
- 'migrations/release_1_0_1.php',
- ),
- ),
- 'service' => array(
- 'default' => true,
- 'dependencies' => array(),
- 'files' => array(
- 'service.php',
- 'config/services.yml',
- 'config/parameters.yml',
- ),
- ),
- 'controller' => array(
- 'default' => true,
- 'dependencies' => array('service', 'route'),
- 'files' => array(
- 'config/services.yml',
- 'event/main_listener.php',
- 'language/en/common.php',
- 'controller/main.php',
- 'styles/prosilver/template/demo_body.html',
- ),
- ),
- 'tests' => array(
- 'default' => true,
- 'dependencies' => array(),
- 'files' => array(
- 'tests/controller/main_test.php',
- 'tests/dbal/fixtures/config.xml',
- 'tests/dbal/simple_test.php',
- 'tests/functional/demo_test.php',
- 'tests/mock/controller_helper.php',
- 'tests/mock/template.php',
- 'tests/mock/user.php',
- 'phpunit.xml.dist',
- ),
- ),
- 'travis' => array(
- 'default' => true,
- 'dependencies' => array('tests'),
- 'files' => array(
- 'travis/prepare-phpbb.sh',
- '.travis.yml',
- ),
- ),
-// 'build' => array(
-// 'default' => true,
-// 'dependencies' => array(),
-// 'files' => array(),
-// ),
- );
- }
+ /** @var packager */
+ protected $packager;
/**
* Constructor
*
* @param user $user User instance (mostly for translation)
- * @param path_helper $path_helper The filesystem object
- * @param string $root_path
+ * @param packager $packager
+ * @param validator $validator
*/
- public function __construct(user $user, path_helper $path_helper, $root_path)
+ public function __construct(user $user, packager $packager, validator $validator)
{
parent::__construct($user);
- $this->validator = new validator($this->user);
- $this->path_helper = $path_helper;
- $this->root_path = $root_path;
+
+ $this->packager = $packager;
+ $this->validator = $validator;
}
/**
@@ -190,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->get_composer_data($dialog, $output);
$this->get_component_data($dialog, $output);
- $this->create_extension();
+ $this->packager->create_extension($this->data);
}
/**
@@ -199,7 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function get_composer_data(DialogHelper $dialog, OutputInterface $output)
{
- $dialog_questions = $this->get_composer_dialog_values();
+ $dialog_questions = $this->packager->get_composer_dialog_values();
foreach ($dialog_questions['extension'] as $value => $default)
{
$this->data['extension'][$value] = $this->get_user_input($dialog, $output, $value, $default);
@@ -234,7 +121,7 @@ protected function get_composer_data(DialogHelper $dialog, OutputInterface $outp
*/
protected function get_component_data(DialogHelper $dialog, OutputInterface $output)
{
- $components = $this->get_component_dialog_values();
+ $components = $this->packager->get_component_dialog_values();
foreach ($components as $component => $details)
{
foreach ($details['dependencies'] as $depends)
@@ -250,56 +137,6 @@ protected function get_component_data(DialogHelper $dialog, OutputInterface $out
}
}
- protected function create_extension()
- {
- $ext_path = $this->root_path . 'store/tmp-ext/' . "{$this->data['extension']['vendor_name']}/{$this->data['extension']['extension_name']}/";
- $filesystem = new Filesystem();
- $filesystem->remove($this->root_path . 'store/tmp-ext/');
- $filesystem->mkdir($ext_path);
-
- $template_engine = new twig($this->path_helper, new config(array(
- 'load_tplcompile' => true,
- 'tpl_allow_php' => false,
- 'assets_version' => null,
- )), $this->user, new context());
- $template_engine->set_custom_style('skeletonextension', $this->root_path . 'ext/phpbb/skeleton/skeleton');
-
- $template_engine->assign_vars(array(
- 'COMPONENT' => $this->data['components'],
- 'EXTENSION' => $this->data['extension'],
- 'REQUIREMENTS' => $this->data['requirements'],
- 'AUTHORS' => $this->data['authors'],
- ));
-
- $component_data = $this->get_component_dialog_values();
- $skeleton_files = array(
- 'license.txt',
- 'README.md',
- );
-
- foreach ($this->data['components'] as $component => $selected)
- {
- if ($selected && !empty($component_data[$component]['files']))
- {
- $skeleton_files = array_merge($skeleton_files, $component_data[$component]['files']);
- }
- }
-
- foreach ($skeleton_files as $file)
- {
- $template_engine->set_filenames(array('body' => $file . '.tpl'));
- $body = $template_engine->assign_display('body');
- if (substr($file, -5) === '.html')
- {
- $body = str_replace('&lt;', '<', $body);
- $body = str_replace('&#123;', '{', $body);
- }
- $filesystem->dumpFile($ext_path . $file, trim($body) . "\n");
- }
-
- $filesystem->dumpFile($ext_path . 'composer.json', $this->get_composer_json_from_data());
- }
-
/**
* @param DialogHelper $dialog
* @param OutputInterface $output
@@ -338,53 +175,4 @@ protected function get_user_input(DialogHelper $dialog, OutputInterface $output,
return $return_value;
}
-
- /**
- * @return string
- */
- protected function get_composer_json_from_data()
- {
- $composer = array(
- 'name' => "{$this->data['extension']['vendor_name']}/{$this->data['extension']['extension_name']}",
- 'type' => 'phpbb-extension',
- 'description' => "{$this->data['extension']['extension_description']}",
- 'homepage' => "{$this->data['extension']['extension_homepage']}",
- 'version' => "{$this->data['extension']['extension_version']}",
- 'time' => "{$this->data['extension']['extension_time']}",
- 'license' => 'GPL-2.0',
- 'authors' => array(),
- 'require' => array(
- 'php' => "{$this->data['requirements']['php_version']}",
- ),
- 'require-dev' => array(
- 'phpbb/epv' => 'dev-master',
- ),
- 'extra' => array(
- 'display-name' => "{$this->data['extension']['extension_display_name']}",
- 'soft-require' => array(
- 'phpbb/phpbb' => "{$this->data['requirements']['phpbb_version_min']},{$this->data['requirements']['phpbb_version_max']}",
- ),
- ),
- );
-
- if ($this->data['components']['build'])
- {
- $composer['require-dev']['phing/phing'] = '2.4.*';
- }
-
- foreach ($this->data['authors'] as $i => $author_data)
- {
- if ($this->data['authors'][$i]['author_name'] !== '')
- {
- $composer['authors'][] = array(
- 'name' => "{$this->data['authors'][$i]['author_name']}",
- 'email' => "{$this->data['authors'][$i]['author_email']}",
- 'homepage' => "{$this->data['authors'][$i]['author_homepage']}",
- 'role' => "{$this->data['authors'][$i]['author_role']}",
- );
- }
- }
-
- return json_encode($composer, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES);
- }
}
View
203 helper/packager.php
@@ -0,0 +1,203 @@
+<?php
+/**
+ *
+ * This file is part of the phpBB Forum Software package.
+ *
+ * @copyright (c) phpBB Limited <https://www.phpbb.com>
+ * @license GNU General Public License, version 2 (GPL-2.0)
+ *
+ * For full copyright and license information, please see
+ * the docs/CREDITS.txt file.
+ *
+ */
+
+namespace phpbb\skeleton\helper;
+
+use phpbb\config\config;
+use phpbb\di\service_collection;
+use phpbb\path_helper;
+use phpbb\template\context;
+use phpbb\template\twig\twig;
+use phpbb\user;
+use Symfony\Component\Filesystem\Filesystem;
+
+class packager
+{
+ protected $data = array();
+
+ /** @var user */
+ protected $user;
+
+ /** @var path_helper */
+ protected $path_helper;
+
+ /** @var service_collection */
+ protected $collection;
+
+ /**
+ * Constructor
+ *
+ * @param user $user User instance (mostly for translation)
+ * @param path_helper $path_helper The filesystem object
+ * @param service_collection $collection
+ * @param string $root_path
+ */
+ public function __construct(user $user, path_helper $path_helper, service_collection $collection, $root_path)
+ {
+ $this->user = $user;
+ $this->path_helper = $path_helper;
+ $this->collection = $collection;
+ $this->root_path = $root_path;
+ }
+
+ /**
+ * @return array
+ */
+ public function get_composer_dialog_values()
+ {
+ return array(
+ 'author' => array(
+ 'author_name' => null,
+ 'author_email' => null,
+ 'author_homepage' => null,
+ 'author_role' => null,
+ ),
+ 'extension' => array(
+ 'vendor_name' => null,
+ 'extension_display_name' => null,
+ 'extension_name' => null,
+ 'extension_description' => null,
+ 'extension_version' => '1.0.0-dev',
+ 'extension_homepage' => null,
+ 'extension_time' => date('Y-m-d'),
+ ),
+ 'requirements' => array(
+ 'php_version' => '>=5.3.3',
+ 'phpbb_version_min' => '>=3.1.4',
+ 'phpbb_version_max' => '<3.2.0@dev',
+ )
+ );
+ }
+
+ /**
+ * @return array
+ */
+ public function get_component_dialog_values()
+ {
+ $components = array();
+ foreach ($this->collection as $service)
+ {
+ /** @var \phpbb\skeleton\skeleton $service */
+ $components[$service->get_name()] = array(
+ 'default' => $service->get_default(),
+ 'dependencies' => $service->get_dependencies(),
+ 'files' => $service->get_files(),
+ );
+ }
+
+ return $components;
+ }
+
+ /**
+ * @param array $data
+ */
+ public function create_extension($data)
+ {
+ $ext_path = $this->root_path . 'store/tmp-ext/' . "{$data['extension']['vendor_name']}/{$data['extension']['extension_name']}/";
+ $filesystem = new Filesystem();
+ $filesystem->remove($this->root_path . 'store/tmp-ext/');
+ $filesystem->mkdir($ext_path);
+
+ $template_engine = new twig($this->path_helper, new config(array(
+ 'load_tplcompile' => true,
+ 'tpl_allow_php' => false,
+ 'assets_version' => null,
+ )), $this->user, new context());
+ $template_engine->set_custom_style('skeletonextension', $this->root_path . 'ext/phpbb/skeleton/skeleton');
+
+ $template_engine->assign_vars(array(
+ 'COMPONENT' => $data['components'],
+ 'EXTENSION' => $data['extension'],
+ 'REQUIREMENTS' => $data['requirements'],
+ 'AUTHORS' => $data['authors'],
+ ));
+
+ $component_data = $this->get_component_dialog_values();
+ $skeleton_files = array(
+ 'license.txt',
+ 'README.md',
+ );
+
+ foreach ($data['components'] as $component => $selected)
+ {
+ if ($selected && !empty($component_data[$component]['files']))
+ {
+ $skeleton_files = array_merge($skeleton_files, $component_data[$component]['files']);
+ }
+ }
+
+ foreach ($skeleton_files as $file)
+ {
+ $template_engine->set_filenames(array('body' => $file . '.tpl'));
+ $body = $template_engine->assign_display('body');
+ if (substr($file, -5) === '.html')
+ {
+ $body = str_replace('&lt;', '<', $body);
+ $body = str_replace('&#123;', '{', $body);
+ }
+ $filesystem->dumpFile($ext_path . $file, trim($body) . "\n");
+ }
+
+ $filesystem->dumpFile($ext_path . 'composer.json', $this->get_composer_json_from_data($data));
+ }
+
+ /**
+ * @param array $data
+ * @return string
+ */
+ public function get_composer_json_from_data($data)
+ {
+ $composer = array(
+ 'name' => "{$data['extension']['vendor_name']}/{$data['extension']['extension_name']}",
+ 'type' => 'phpbb-extension',
+ 'description' => "{$data['extension']['extension_description']}",
+ 'homepage' => "{$data['extension']['extension_homepage']}",
+ 'version' => "{$data['extension']['extension_version']}",
+ 'time' => "{$data['extension']['extension_time']}",
+ 'license' => 'GPL-2.0',
+ 'authors' => array(),
+ 'require' => array(
+ 'php' => "{$data['requirements']['php_version']}",
+ ),
+ 'require-dev' => array(
+ 'phpbb/epv' => 'dev-master',
+ ),
+ 'extra' => array(
+ 'display-name' => "{$data['extension']['extension_display_name']}",
+ 'soft-require' => array(
+ 'phpbb/phpbb' => "{$data['requirements']['phpbb_version_min']},{$data['requirements']['phpbb_version_max']}",
+ ),
+ ),
+ );
+
+ if ($data['components']['build'])
+ {
+ $composer['require-dev']['phing/phing'] = '2.4.*';
+ }
+
+ foreach ($data['authors'] as $i => $author_data)
+ {
+ if ($data['authors'][$i]['author_name'] !== '')
+ {
+ $composer['authors'][] = array(
+ 'name' => "{$data['authors'][$i]['author_name']}",
+ 'email' => "{$data['authors'][$i]['author_email']}",
+ 'homepage' => "{$data['authors'][$i]['author_homepage']}",
+ 'role' => "{$data['authors'][$i]['author_role']}",
+ );
+ }
+ }
+
+ return json_encode($composer, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES);
+ }
+}
View
2 language/en/common.php
@@ -23,6 +23,7 @@
$lang = array_merge($lang, array(
'CLI_DESCRIPTION_SKELETON_CREATE' => 'A console command to create a basic extension',
+ 'PHPBB_SKELETON_EXT' => 'Create skeleton extension',
'SKELETON_QUESTION_VENDOR_NAME' => 'Please enter the vendor name (starting with a letter, letters and numbers only): ',
'SKELETON_QUESTION_EXTENSION_DISPLAY_NAME' => 'Please enter the display (readable) name of your extension: ',
@@ -47,6 +48,7 @@
'SKELETON_QUESTION_COMPONENT_ACP' => 'Should we add a sample for a administration module (default: y) [y/n]: ',
'SKELETON_QUESTION_COMPONENT_MIGRATION' => 'Should we add a sample for a database migration (default: y) [y/n]: ',
'SKELETON_QUESTION_COMPONENT_SERVICE' => 'Should we add a sample for a service (default: y) [y/n]: ',
+ 'SKELETON_QUESTION_COMPONENT_CONTROLLER' => 'Should we add a sample for a controller (default: y) [y/n]: ',
'SKELETON_QUESTION_COMPONENT_TESTS' => 'Should we add a sample for a phpunit tests (default: y) [y/n]: ',
'SKELETON_QUESTION_COMPONENT_TRAVIS' => 'Should we add a sample for test execution on Travis CI (default: y) [y/n]: ',
'SKELETON_QUESTION_COMPONENT_BUILD' => 'Should we add a sample script for building packages for the customisation database (default: n) [y/n]: ',
View
63 skeleton.php
@@ -0,0 +1,63 @@
+<?php
+/**
+ *
+ * This file is part of the phpBB Forum Software package.
+ *
+ * @copyright (c) phpBB Limited <https://www.phpbb.com>
+ * @license GNU General Public License, version 2 (GPL-2.0)
+ *
+ * For full copyright and license information, please see
+ * the docs/CREDITS.txt file.
+ *
+ */
+
+namespace phpbb\skeleton;
+
+class skeleton
+{
+ /** @var string */
+ protected $name;
+
+ /** @var bool */
+ protected $default;
+
+ /** @var array */
+ protected $dependencies;
+
+ /** @var array */
+ protected $files;
+
+ /**
+ * @param string $name
+ * @param bool $default
+ * @param array $dependencies
+ * @param array $files
+ */
+ public function __construct($name, $default, array $dependencies, array $files)
+ {
+ $this->name = $name;
+ $this->default = $default;
+ $this->dependencies = $dependencies;
+ $this->files = $files;
+ }
+
+ public function get_name()
+ {
+ return $this->name;
+ }
+
+ public function get_default()
+ {
+ return $this->default;
+ }
+
+ public function get_dependencies()
+ {
+ return $this->dependencies;
+ }
+
+ public function get_files()
+ {
+ return $this->files;
+ }
+}

0 comments on commit f0517b5

Please sign in to comment.