From 9a02b6f5cf0376ffe853c12a4feb60c5a2be8820 Mon Sep 17 00:00:00 2001
From: dyszczo <krzysztof@dyszczyk.pl>
Date: Wed, 16 May 2018 21:29:09 +0200
Subject: [PATCH] more like builder pattern

---
 src/Builder/AbstractBuilder.php             | 30 +++----------
 src/Builder/InfoBuilder.php                 | 49 ++++++++++++++-------
 src/Plugin/Exception/ClassAlreadyExists.php |  8 ++++
 src/Plugin/Exception/ClassNotExists.php     |  8 ++++
 src/Plugin/Storage.php                      | 32 ++++++++++++++
 src/Plugin/StorageFactory.php               | 14 ++++++
 6 files changed, 101 insertions(+), 40 deletions(-)
 create mode 100644 src/Plugin/Exception/ClassAlreadyExists.php
 create mode 100644 src/Plugin/Exception/ClassNotExists.php
 create mode 100644 src/Plugin/Storage.php
 create mode 100644 src/Plugin/StorageFactory.php

diff --git a/src/Builder/AbstractBuilder.php b/src/Builder/AbstractBuilder.php
index 296089c..d10de55 100644
--- a/src/Builder/AbstractBuilder.php
+++ b/src/Builder/AbstractBuilder.php
@@ -2,32 +2,16 @@
 
 namespace WPDesk\PluginBuilder\Builder;
 
-use WPDesk\PluginBuilder\Plugin\AbstractPlugin;
-
 class AbstractBuilder {
-	protected static $instances = [];
+	public function build() {
+	}
+
+	public function store_plugin() {
+	}
 
-	/**
-	 * @param string $class
-	 * @param AbstractPlugin $object
-	 */
-	protected function addToStorage( $class, $object ) {
-		if ( isset( self::$instances[ $class ] ) ) {
-			throw new Exception\ClassAlreadyExists( "Class {$class} already exists" );
-		}
-		self::$instances[ $class ] = $object;
+	public function init_plugin() {
 	}
 
-	/**
-	 * @param string $class
-	 *
-	 * @return AbstractPlugin
-	 */
-	protected function getFromStorage( $class ) {
-		if ( isset( self::$instances[ $class ] ) ) {
-			return self::$instances[ $class ];
-		} else {
-			throw new Exception\ClassNotExists( "Class {$class} not exists in storage" );
-		}
+	public function get_plugin() {
 	}
 }
\ No newline at end of file
diff --git a/src/Builder/InfoBuilder.php b/src/Builder/InfoBuilder.php
index c3e75f3..d878551 100644
--- a/src/Builder/InfoBuilder.php
+++ b/src/Builder/InfoBuilder.php
@@ -3,40 +3,55 @@
 namespace WPDesk\PluginBuilder\Builder;
 
 use WPDesk\PluginBuilder\Plugin\AbstractPlugin;
+use WPDesk\PluginBuilder\Plugin\PluginStorage;
 
 class InfoBuilder extends AbstractBuilder {
 	const FILTER_PLUGIN_CLASS = 'wp_builder_plugin_class';
 	const HOOK_BEFORE_PLUGIN_INIT = 'wp_builder_before_plugin_init';
 	const HOOK_AFTER_PLUGIN_INIT = 'wp_builder_before_init';
 
+	/** @var AbstractPlugin */
+	private $plugin;
+
+	/** @var \WPDesk_Buildable */
+	private $info;
+
+	/** @var PluginStorage */
+	private $storage;
+
+	/** @var string  */
+	protected $storage_id;
+
+	public function __construct( \WPDesk_Buildable $info, PluginStorage $storage ) {
+		$this->info = $info;
+		$this->storage = $storage;
+		$this->storage_id = $info->get_class_name();
+	}
+
 	/**
 	 * Builds instance of plugin
-	 *
-	 * @param \WPDesk_Plugin_Info $info
-	 * @return AbstractPlugin
-	 *
-	 * @return AbstractPlugin
 	 */
-	public function build_from_info( \WPDesk_Buildable $info ) {
-		$class_name = apply_filters( self::FILTER_PLUGIN_CLASS, $info->get_class_name() );
+	public function build_plugin() {
+		$class_name = apply_filters( self::FILTER_PLUGIN_CLASS, $this->info->get_class_name() );
 
 		/** @var AbstractPlugin $plugin */
-		$plugin = new $class_name( $info );
-		$this->addToStorage( $info->get_class_name(), $plugin );
+		$this->plugin = new $class_name( $this->info );
+	}
 
-		do_action( self::HOOK_BEFORE_PLUGIN_INIT, $plugin );
-		$plugin->init();
-		do_action( self::HOOK_AFTER_PLUGIN_INIT, $plugin );
+	public function store_plugin() {
+		$this->storage->add_to_storage( $this->storage_id, $this->plugin );
+	}
 
-		return $plugin;
+	public function init_plugin() {
+		do_action( self::HOOK_BEFORE_PLUGIN_INIT, $this->plugin );
+		$this->plugin->init();
+		do_action( self::HOOK_AFTER_PLUGIN_INIT, $this->plugin );
 	}
 
 	/**
-	 * @param string $class
-	 *
 	 * @return AbstractPlugin
 	 */
-	public function get_plugin_instance( $class ) {
-		return $this->getFromStorage( $class );
+	public function get_plugin() {
+		return $this->plugin;
 	}
 }
\ No newline at end of file
diff --git a/src/Plugin/Exception/ClassAlreadyExists.php b/src/Plugin/Exception/ClassAlreadyExists.php
new file mode 100644
index 0000000..7d05c46
--- /dev/null
+++ b/src/Plugin/Exception/ClassAlreadyExists.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace WPDesk\PluginBuilder\Plugin\Exception;
+
+class ClassAlreadyExists extends \RuntimeException
+{
+
+}
\ No newline at end of file
diff --git a/src/Plugin/Exception/ClassNotExists.php b/src/Plugin/Exception/ClassNotExists.php
new file mode 100644
index 0000000..a10987b
--- /dev/null
+++ b/src/Plugin/Exception/ClassNotExists.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace WPDesk\PluginBuilder\Plugin\Exception;
+
+class ClassNotExists extends \RuntimeException
+{
+
+}
\ No newline at end of file
diff --git a/src/Plugin/Storage.php b/src/Plugin/Storage.php
new file mode 100644
index 0000000..6342b50
--- /dev/null
+++ b/src/Plugin/Storage.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace WPDesk\PluginBuilder\Plugin;
+
+class PluginStorage {
+	protected static $instances = [];
+
+	/**
+	 * @param string $class
+	 * @param AbstractPlugin $object
+	 */
+	public function add_to_storage( $class, $object ) {
+		if ( isset( self::$instances[ $class ] ) ) {
+			throw new Exception\ClassAlreadyExists( "Class {$class} already exists" );
+		}
+		self::$instances[ $class ] = $object;
+	}
+
+	/**
+	 * @param string $class
+	 *
+	 * @return AbstractPlugin
+	 */
+	public function get_from_storage( $class ) {
+		if ( isset( self::$instances[ $class ] ) ) {
+			return self::$instances[ $class ];
+		} else {
+			throw new Exception\ClassNotExists( "Class {$class} not exists in storage" );
+		}
+	}
+}
+
diff --git a/src/Plugin/StorageFactory.php b/src/Plugin/StorageFactory.php
new file mode 100644
index 0000000..cdfd229
--- /dev/null
+++ b/src/Plugin/StorageFactory.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace WPDesk\PluginBuilder\Plugin;
+
+class StorageFactory {
+
+	/**
+	 * @return PluginStorage
+	 */
+	public function create_storage() {
+		return new PluginStorage();
+	}
+}
+
-- 
GitLab