zend framework2 - DRY zf2 Module.php -
is there standard way dry module.php? have several simple module.php files this
<?php namespace solimporter; class module { public function getautoloaderconfig() { return array( 'zend\loader\standardautoloader' => array( 'namespaces' => array( __namespace__ => __dir__ . '/src/' . __namespace__, ), ), ); } public function getconfig() { return include __dir__ . '/config/module.config.php'; } }
only namespace different , of course phpcpd complains this.
you can this. however, create dependency not needed module. , furthermore, cannot __dir__
of child class without reflection. , reflection can harm performance, if use every module class (and every module class is, principle, loaded on every request).
but here goes:
namespace mycommon\module; class abstractmodule { public function getautoloaderconfig() { $dir = $this->getdir(); $namespace = $this->getnamespace(); return array( 'zend\loader\standardautoloader' => array( 'namespaces' => array( $namespace => $dir . '/src/' . $namespace, ), ), ); } public function getconfig() { $dir = $this->getdir(); return include $dir . '/config/module.config.php'; } protected function getdir() { $reflection = new reflectionclass($this); return dirname($reflection->getfilename()); } protected function getnamespace() { return str_replace('\module', '', get_class($this)); } }
use like:
namespace mymodule; use mycommon\module\abstractmodule; class module extends abstractmodule { }
Comments
Post a Comment