diff --git a/Module.php b/Module.php index 9bd7995..ac8616d 100644 --- a/Module.php +++ b/Module.php @@ -43,11 +43,11 @@ use Zend\Http\Request as HttpRequest; use Zend\ModuleManager\Feature; use Zend\Mvc\MvcEvent; -use SlmGoogleAnalytics\Analytics; -use SlmGoogleAnalytics\View\Helper; class Module implements -Feature\AutoloaderProviderInterface, Feature\ConfigProviderInterface, Feature\ViewHelperProviderInterface, Feature\ServiceProviderInterface, Feature\BootstrapListenerInterface + Feature\AutoloaderProviderInterface, + Feature\ConfigProviderInterface, + Feature\BootstrapListenerInterface { public function getAutoloaderConfig() @@ -69,70 +69,6 @@ public function getConfig() return include __DIR__ . '/config/module.config.php'; } - public function getViewHelperConfig() - { - return array( - 'factories' => array( - 'googleAnalytics' => function($sm) { - $script = $sm->getServiceLocator()->get('google-analytics-script'); - $helper = new Helper\GoogleAnalytics($script); - - return $helper; - }, - ), - ); - } - - public function getServiceConfig() - { - return array( - 'aliases' => array( - 'google-analytics' => 'SlmGoogleAnalytics\Analytics\Tracker', - ), - 'invokables' => array( - 'google-analytics-script-analytics-js' => 'SlmGoogleAnalytics\View\Helper\Script\Analyticsjs', - 'google-analytics-script-ga-js' => 'SlmGoogleAnalytics\View\Helper\Script\Gajs', - ), - 'factories' => array( - 'SlmGoogleAnalytics\Analytics\Tracker' => function($sm) { - $config = $sm->get('config'); - $config = $config['google_analytics']; - - $tracker = new Analytics\Tracker($config['id']); - - if (isset($config['domain_name'])) { - $tracker->setDomainName($config['domain_name']); - } - - if (isset($config['allow_linker'])) { - $tracker->setAllowLinker($config['allow_linker']); - } - - if (true === $config['anonymize_ip']) { - $tracker->setAnonymizeIp(true); - } - - if (false === $config['enable']) { - $tracker->setEnableTracking(false); - } - - return $tracker; - }, - 'google-analytics-script' => function($sm) { - $config = $sm->get('config'); - $scriptName = $config['google_analytics']['script']; - - $script = $sm->get($scriptName); - $ga = $sm->get('google-analytics'); - - $script->setTracker($ga); - - return $script; - }, - ), - ); - } - /** * When the render event is triggered, we invoke the view helper to * render the javascript code. diff --git a/config/module.config.php b/config/module.config.php index 7001952..d8c1c4c 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -47,4 +47,22 @@ 'anonymize_ip' => false, 'script' => 'google-analytics-script-ga-js', ), + 'service_manager' => array( + 'aliases' => array( + 'google-analytics' => 'SlmGoogleAnalytics\Analytics\Tracker', + ), + 'invokables' => array( + 'google-analytics-script-analytics-js' => 'SlmGoogleAnalytics\View\Helper\Script\Analyticsjs', + 'google-analytics-script-ga-js' => 'SlmGoogleAnalytics\View\Helper\Script\Gajs', + ), + 'factories' => array( + 'SlmGoogleAnalytics\Analytics\Tracker' => 'SlmGoogleAnalytics\Service\TrackerFactory', + 'google-analytics-script' => 'SlmGoogleAnalytics\Service\ScriptFactory', + ), + ), + 'view_helpers' => array( + 'factories' => array( + 'googleAnalytics' => 'SlmGoogleAnalytics\Service\GoogleAnalyticsFactory', + ), + ) ); diff --git a/src/SlmGoogleAnalytics/Service/GoogleAnalyticsFactory.php b/src/SlmGoogleAnalytics/Service/GoogleAnalyticsFactory.php new file mode 100644 index 0000000..9d4c9a6 --- /dev/null +++ b/src/SlmGoogleAnalytics/Service/GoogleAnalyticsFactory.php @@ -0,0 +1,56 @@ + + * @copyright 2012-2013 Jurian Sluiman. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://www.psd2html.pl + */ +namespace SlmGoogleAnalytics\Service; + +use Zend\ServiceManager\FactoryInterface; +use Zend\ServiceManager\ServiceLocatorInterface; +use SlmGoogleAnalytics\View\Helper\GoogleAnalytics; + +class GoogleAnalyticsFactory implements FactoryInterface +{ + public function createService(ServiceLocatorInterface $serviceLocator) + { + $sm = $serviceLocator->getServiceLocator(); + $script = $sm->get('google-analytics-script'); + $helper = new GoogleAnalytics($script); + + return $helper; + } +} diff --git a/src/SlmGoogleAnalytics/Service/ScriptFactory.php b/src/SlmGoogleAnalytics/Service/ScriptFactory.php new file mode 100644 index 0000000..c51d4a5 --- /dev/null +++ b/src/SlmGoogleAnalytics/Service/ScriptFactory.php @@ -0,0 +1,62 @@ + + * @copyright 2012-2013 Jurian Sluiman. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://www.psd2html.pl + */ +namespace SlmGoogleAnalytics\Service; + +use Zend\ServiceManager\FactoryInterface; +use Zend\ServiceManager\ServiceLocatorInterface; + +class ScriptFactory implements FactoryInterface +{ + + public function createService(ServiceLocatorInterface $serviceLocator) + { + $config = $serviceLocator->get('config'); + $scriptName = $config['google_analytics']['script']; + + + $script = $serviceLocator->get($scriptName); + /* @var $script \SlmGoogleAnalytics\View\Helper\Script\ScriptInterface */ + $ga = $serviceLocator->get('google-analytics'); + + $script->setTracker($ga); + + return $script; + } +} diff --git a/src/SlmGoogleAnalytics/Service/TrackerFactory.php b/src/SlmGoogleAnalytics/Service/TrackerFactory.php new file mode 100644 index 0000000..543211b --- /dev/null +++ b/src/SlmGoogleAnalytics/Service/TrackerFactory.php @@ -0,0 +1,74 @@ + + * @copyright 2012-2013 Jurian Sluiman. + * @license http://www.opensource.org/licenses/bsd-license.php BSD License + * @link http://www.psd2html.pl + */ +namespace SlmGoogleAnalytics\Service; + +use Zend\ServiceManager\FactoryInterface; +use Zend\ServiceManager\ServiceLocatorInterface; +use SlmGoogleAnalytics\Analytics\Tracker; + +class TrackerFactory implements FactoryInterface +{ + + public function createService(ServiceLocatorInterface $serviceLocator) + { + $config = $serviceLocator->get('config'); + $gaConfig = $config['google_analytics']; + + $tracker = new Tracker($gaConfig['id']); + + if (isset($gaConfig['domain_name'])) { + $tracker->setDomainName($gaConfig['domain_name']); + } + + if (isset($gaConfig['allow_linker'])) { + $tracker->setAllowLinker($gaConfig['allow_linker']); + } + + if (true === $gaConfig['anonymize_ip']) { + $tracker->setAnonymizeIp(true); + } + + if (false === $gaConfig['enable']) { + $tracker->setEnableTracking(false); + } + + return $tracker; + } +}