injector = $injector; $this->event_manager = $event_manager; $this->config = $config; // $default = array( // 'sharing' => array(), // 'aliases' => array(), // 'definitions' => array(), // 'define_param' => array(), // 'delegations' => array(), // 'preparations' => array(), // 'concretes' => array(), // 'options_concretes' => array(), // 'subscribers' => array(), // ); // $this->app = array_merge( $default, $app ); $this->app = $app; $this->loaded = false; } /** * Init $injector * * @param array $app Array with classes to be initialized. */ public function init( array $app ) { foreach ( $app['sharing'] as $class ) { $this->injector->share( $class ); } foreach ( $app['aliases'] as $interface => $implementation ) { $this->injector->alias( $interface, $implementation ); } foreach ( $app['definitions'] as $class_name => $class_args ) { $this->injector->define( $class_name, $class_args ); } foreach ( $app['define_param'] as $param_name => $param_args ) { $this->injector->defineParam( $param_name, $param_args ); } foreach ( $app['delegations'] as $param => $callable ) { $this->injector->delegate( $param, $callable ); } foreach ( $app['preparations'] as $class => $callable ) { $this->injector->prepare( $class, $callable ); } } /** * Instantiate classes * * @param array $app Array with classes to be instantiated. */ public function make( array $app ) { foreach ( $app['concretes'] as $concrete ) { $this->event_manager->add_subscriber( $this->injector->make( $concrete ) ); } foreach ( $app['options_concretes'] as $option_name => $concrete ) { if ( empty( $this->config[ $option_name ] ) ) { continue; } $this->event_manager->add_subscriber( $this->injector->make( $concrete ) ); } foreach ( $app['subscribers'] as $option_name => $subscriber ) { if ( is_int( $option_name ) ) { $this->event_manager->add_subscriber( $this->injector->make( $subscriber ) ); continue; } if ( empty( $this->config[ $option_name ] ) ) { continue; } $this->event_manager->add_subscriber( $this->injector->make( $subscriber ) ); } foreach ( $app['execute'] as $callableOrMethodStr => $args ) { try { $this->injector->execute( $callableOrMethodStr, $args ); } catch ( \Exception $exception ) { echo $exception->getMessage(); } } } /** * Load method * * @param string $value [description] * @return string [description] */ public function load() { if ( $this->loaded ) { return; } do_action( 'italystrap_before_injector_loaded' ); $app = $this->get_app(); $this->init( $app ); $this->make( $app ); $this->loaded = true; do_action( 'italystrap_after_injector_loaded', $this->injector, $this->event_manager ); /** * Fires once ItalyStrap plugin has loaded. * * @since 2.7.0 */ do_action( 'italystrap_plugin_app_loaded' ); } /** * Get Shared Classes * * @return array An array with shared classes */ private function get_app() { $default = array( 'sharing' => array(), 'aliases' => array(), 'definitions' => array(), 'define_param' => array(), 'delegations' => array(), 'preparations' => array(), 'concretes' => array(), 'options_concretes' => array(), 'subscribers' => array(), 'execute' => array(), ); $this->app = (array) apply_filters( 'italystrap_app', $this->app ); $this->app = array_merge( $default, $this->app ); return $this->app; } /** * Get Shared Classes * * @return array An array with shared classes */ private function get( $key ) { return (array) apply_filters( "italystrap_{$key}", $this->app[ $key ] ); } }