Beispiel #1
0
 public function testBasePathWithFilePrefixedBySlash()
 {
     $helper = new BasePath();
     $helper->setBasePath('/foo');
     
     $this->assertEquals('/foo/bar', $helper('/bar'));
 }
 /**
  * Create and return the view helper manager
  *
  * @param  ServiceLocatorInterface $serviceLocator
  * @return ViewHelperInterface
  * @throws Exception\RuntimeException
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $plugins = parent::createService($serviceLocator);
     foreach ($this->defaultHelperMapClasses as $configClass) {
         if (is_string($configClass) && class_exists($configClass)) {
             $config = new $configClass();
             if (!$config instanceof ConfigInterface) {
                 throw new Exception\RuntimeException(sprintf('Invalid service manager configuration class provided; received "%s", expected class implementing %s', $configClass, 'Zend\\ServiceManager\\ConfigInterface'));
             }
             $config->configureServiceManager($plugins);
         }
     }
     // Configure URL view helper with router
     $plugins->setFactory('url', function () use($serviceLocator) {
         $helper = new ViewHelper\Url();
         $router = Console::isConsole() ? 'HttpRouter' : 'Router';
         $helper->setRouter($serviceLocator->get($router));
         $match = $serviceLocator->get('application')->getMvcEvent()->getRouteMatch();
         if ($match instanceof RouteMatch) {
             $helper->setRouteMatch($match);
         }
         return $helper;
     });
     $plugins->setFactory('basepath', function () use($serviceLocator) {
         $config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : array();
         $basePathHelper = new ViewHelper\BasePath();
         if (isset($config['view_manager']) && isset($config['view_manager']['base_path'])) {
             $basePathHelper->setBasePath($config['view_manager']['base_path']);
         } else {
             $request = $serviceLocator->get('Request');
             if (is_callable(array($request, 'getBasePath'))) {
                 $basePathHelper->setBasePath($request->getBasePath());
             }
         }
         return $basePathHelper;
     });
     /**
      * Configure doctype view helper with doctype from configuration, if available.
      *
      * Other view helpers depend on this to decide which spec to generate their tags
      * based on. This is why it must be set early instead of later in the layout phtml.
      */
     $plugins->setFactory('doctype', function () use($serviceLocator) {
         $config = $serviceLocator->has('Config') ? $serviceLocator->get('Config') : array();
         $config = isset($config['view_manager']) ? $config['view_manager'] : array();
         $doctypeHelper = new ViewHelper\Doctype();
         if (isset($config['doctype']) && $config['doctype']) {
             $doctypeHelper->setDoctype($config['doctype']);
         }
         return $doctypeHelper;
     });
     return $plugins;
 }
Beispiel #3
0
 /**
  * @return BasePathHelper
  */
 protected function getBasePathHelper()
 {
     if (null === $this->basePathHelper) {
         $renderer = $this->getView();
         if (method_exists($renderer, 'plugin')) {
             $this->basePathHelper = $renderer->plugin($this->defaultBasePathHelper);
         }
         if (!$this->basePathHelper instanceof BasePathHelper) {
             $this->basePathHelper = new BasePathHelper();
             $this->basePathHelper->setBasePath('/');
             $this->basePathHelper->setView($this->getView());
         }
     }
     return $this->basePathHelper;
 }
Beispiel #4
0
 /**
  * Appends the required configs in a head script
  */
 public function __invoke()
 {
     $namespaces = $this->bootstrap->getPaths();
     if ($namespaces) {
         foreach ($namespaces as $namespace => $path) {
             if ($path[0] !== '/') {
                 $namespaces[$namespace] = $this->basePath->__invoke($path);
             }
         }
         $data = array('enabled' => true, 'paths' => $namespaces);
         $this->headScript->appendScript('Ext.Loader.setConfig(' . json_encode($data) . ');');
     }
     if ($requires = $this->bootstrap->getRequires()) {
         $this->headScript->appendScript('Ext.syncRequire(' . json_encode($requires) . ');');
     }
 }
 /**
  * Create and return the view helper manager
  *
  * @param  ServiceLocatorInterface $serviceLocator
  * @return ViewHelperInterface
  * @throws Exception\RuntimeException
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $pluginManagerClass = static::PLUGIN_MANAGER_CLASS;
     $plugins = new $pluginManagerClass();
     $plugins->setServiceLocator($serviceLocator);
     $configuration = $serviceLocator->get('Config');
     if (isset($configuration['di']) && $serviceLocator->has('Di')) {
         $di = $serviceLocator->get('Di');
         $plugins->addAbstractFactory(new DiAbstractServiceFactory($di, DiAbstractServiceFactory::USE_SL_BEFORE_DI));
     }
     foreach ($this->defaultHelperMapClasses as $configClass) {
         if (is_string($configClass) && class_exists($configClass)) {
             $config = new $configClass();
         }
         if (!$config instanceof ConfigInterface) {
             throw new Exception\RuntimeException(sprintf('Invalid service manager configuration class provided; received "%s", expected class implementing %s', $configClass, 'Zend\\ServiceManager\\ConfigInterface'));
         }
         $config->configureServiceManager($plugins);
     }
     // Configure URL view helper with router
     $plugins->setFactory('url', function ($sm) use($serviceLocator) {
         $helper = new ViewHelper\Url();
         $helper->setRouter($serviceLocator->get('Router'));
         $match = $serviceLocator->get('application')->getMvcEvent()->getRouteMatch();
         if ($match instanceof RouteMatch) {
             $helper->setRouteMatch($match);
         }
         return $helper;
     });
     $plugins->setFactory('basepath', function ($sm) use($serviceLocator) {
         $config = $serviceLocator->get('Config');
         $config = $config['view_manager'];
         $basePathHelper = new ViewHelper\BasePath();
         if (isset($config['base_path'])) {
             $basePath = $config['base_path'];
         } else {
             $basePath = $serviceLocator->get('Request')->getBasePath();
         }
         $basePathHelper->setBasePath($basePath);
         return $basePathHelper;
     });
     /**
      * Configure doctype view helper with doctype from configuration, if available.
      *
      * Other view helpers depend on this to decide which spec to generate their tags
      * based on. This is why it must be set early instead of later in the layout phtml.
      */
     $plugins->setFactory('doctype', function ($sm) use($serviceLocator) {
         $config = $serviceLocator->get('Config');
         $config = $config['view_manager'];
         $doctypeHelper = new ViewHelper\Doctype();
         if (isset($config['doctype'])) {
             $doctypeHelper->setDoctype($config['doctype']);
         }
         return $doctypeHelper;
     });
     return $plugins;
 }
Beispiel #6
0
 public function getServiceConfig()
 {
     return array('initializers' => array(function ($instance, $services) {
         if (!Console::isConsole()) {
             return;
         }
         if (!$instance instanceof HelperPluginManager) {
             return;
         }
         $instance->setFactory('basepath', function ($sm) use($services) {
             $config = $services->get('Config');
             $config = $config['view_manager'];
             $basePathHelper = new ViewHelper\BasePath();
             $basePath = '/';
             if (isset($config['base_path'])) {
                 $basePath = $config['base_path'];
             }
             $basePathHelper->setBasePath($basePath);
             return $basePathHelper;
         });
     }));
 }
 public function setUp()
 {
     $basePathHelper = new BasePath();
     $basePathHelper->setBasePath('/my/base/');
     $this->basePathFilter = new BasePathFilter($basePathHelper);
 }
 /**
  * Create and return a factory for creating a BasePath helper.
  *
  * Uses configuration and request services to configure the helper.
  *
  * @param ContainerInterface $services
  * @return callable
  */
 private function createBasePathHelperFactory(ContainerInterface $services)
 {
     return function () use($services) {
         $config = $services->has('config') ? $services->get('config') : [];
         $helper = new ViewHelper\BasePath();
         if (isset($config['view_manager']['base_path_console'])) {
             $helper->setBasePath($config['view_manager']['base_path_console']);
             return $helper;
         }
         if (isset($config['view_manager']) && isset($config['view_manager']['base_path'])) {
             $helper->setBasePath($config['view_manager']['base_path']);
             return $helper;
         }
         $request = $services->get('Request');
         if (is_callable([$request, 'getBasePath'])) {
             $helper->setBasePath($request->getBasePath());
         }
         return $helper;
     };
 }