/** * {@inheritDoc} * @see \Symfony\Component\Translation\Loader\LoaderInterface::load() */ public function load($resource, $locale, $domain = 'messages') { $repo = $this->service->getResourceRepository(); if (!$repo->contains($resource)) { throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource)); } // find file in puli repo $file = $repo->get($resource); $json = $file->getBody(); $data = Json::decode($json); $messages = []; // flatten plural strings foreach ($data as $key => $value) { if (is_array($value)) { $vals = []; foreach ($value as $k => $v) { $vals[] = sprintf('%s: %s', $k, $v); } $val = implode('|', $vals); } else { $val = $value; } $messages[$key] = str_replace(['{{', '}}'], '%', $val); } // put them into message catalog $catalogue = new MessageCatalogue($locale); $catalogue->add($messages, $domain); return $catalogue; }
public function __construct(ServiceContainer $service, $locale) { parent::__construct($locale); $this->service = $service; $dispatcher = $service->getDispatcher(); $dispatcher->addSubscriber($this); $this->domainStack = new Stack(); }
private function getFile($packageName) { $repo = $this->service->getResourceRepository(); try { return $repo->get('/packages/' . $packageName . '/composer.json'); } catch (ResourceNotFoundException $e) { throw new PackageException(sprintf('Package (%s) not found', $packageName)); } }
/** * Load a locale file into the translator * * @param string $file * @param string $domain */ public function loadLocaleFile($file, $domain) { $filename = $this->findLocaleFile($file); if ($filename !== null) { $locale = $this->getLocaleFromFilename($filename); $translator = $this->service->getTranslator(); $translator->addResource('json', $filename, $locale, $domain); } }
public function __construct(ServiceContainer $service) { $this->service = $service; $this->loadedModules = new Map(); $this->activatedModules = new Map(); $this->installedModules = new Map(); $dispatcher = $service->getDispatcher(); $dispatcher->addSubscriber($this); // load modules $modules = ModuleQuery::create()->find(); foreach ($modules as $module) { $this->installedModules->set($module->getName(), $module); if ($module->getActivatedVersion() !== null) { $this->activatedModules->set($module->getName(), $module); } } }
private function registerListeners() { $reg = $this->service->getExtensionRegistry(); $listeners = $reg->getExtensions(CoreModule::EXT_LISTENER); $map = new Map(); $getClass = function ($className) use($map) { if (class_exists($className)) { if ($map->has($className)) { $class = $map->get($className); } else { $class = new $className(); $map->set($className, $class); } if ($class instanceof KeekoEventListenerInterface) { $class->setServiceContainer($this->service); } return $class; } return null; }; foreach ($listeners as $listener) { // subscriber first if (isset($listener['subscriber'])) { $className = $listener['subscriber']; $subscriber = $getClass($className); if ($subscriber !== null && $subscriber instanceof KeekoEventSubscriberInterface) { $this->dispatcher->addSubscriber($subscriber); } } // class if (isset($listener['class']) && isset($listener['method']) && isset($listener['event'])) { $className = $listener['class']; $class = $getClass($className); if ($class !== null && $class instanceof KeekoEventListenerInterface && method_exists($class, $listener['method'])) { $this->dispatcher->addListener($listener['event'], [$class, $listener['method']]); } } } }
public function validate($model) { $builder = new ValidatorBuilder(); $builder->setTranslator($this->service->getTranslator()); $validator = $builder->getValidator(); $validations = $this->getValidations(); $this->violations = new ConstraintViolationList(); foreach ($validations as $column => $validation) { $method = 'get' . NameUtils::toStudlyCase($column); if (method_exists($model, $method)) { $value = $model->{$method}(); $constraints = []; foreach ($validation as $options) { $name = $options['constraint']; unset($options['constraint']); $constraints[] = $this->getConstraint($name, $options); } $violations = $validator->validate($value, $constraints); $this->violations->addAll($violations); } } return (bool) (!(count($this->violations) > 0)); }
public function build(array $data = []) { $prefs = $this->service->getPreferenceLoader()->getSystemPreferences(); $data = array_merge($data, ['login_label' => $prefs->getUserLogin()]); return $this->render('/keeko/user/templates/login-form.twig', $data); }
/** * * @return Response */ public function process() { try { $request = Request::createFromGlobals(); // no trailing slashes in urls // redirect unless it's the root url $syspref = $this->service->getPreferenceLoader()->getSystemPreferences(); if (substr($request->getUri(), -1) == '/' && substr($request->getUri(), 0, -1) != $syspref->getRootUrl()) { $this->redirect(rtrim($request->getUri(), '/')); } $router = new ApplicationRouter(); $uri = $router->match($request); $model = $uri->getApplication(); // printf('<p><br><br></p><p>Basepath: %s<br> // Pathinfo: %s<br> // Baseurl: %s<br> // Host: %s<br> // HttpHost: %s<br> // Requsturi: %s<br> // Uri: %s<br> // Port: %s<br> // Secure: %s<br><br>APP<br> // Basepath: %s<br> // Host: %s<br> // Secure: %s // </p>', // $request->getBasePath(), // $request->getPathInfo(), // $request->getBaseUrl(), // $request->getHost(), // $request->getHttpHost(), // $request->getRequestUri(), // $request->getUri(), // $request->getPort(), // $request->isSecure() ? 'yes' : 'no', // $uri->getBasepath(), // $uri->getHttphost(), // $uri->getSecure() ? 'yes' : 'no'); // set locale $localization = $uri->getLocalization(); $this->service->getLocaleService()->setLocale($localization->getLocale()); // init app $class = $model->getClassName(); $app = new $class($model, $uri, $this->service); $app->setBaseUrl($uri->getUrl()); $app->setDestination($router->getDestination()); // $app->setRootUrl($root); // $app->setAppPath($router->getPrefix()); // $app->setDestinationPath($router->getDestination()); $this->app = $app; $response = $this->handle($app, $request); if ($response instanceof RedirectResponse) { $response->sendHeaders(); $this->redirect($response->getTargetUrl()); } $response->prepare($request); return $response; } catch (\Exception $e) { printf('<b>[%s] %s</b><pre>%s</pre>', get_class($e), $e->getMessage(), $e->getTraceAsString()); } }
/** * * @param string $packageName * @return PackageSchema */ protected function getPackageSchema($packageName) { return $this->service->getPackageManager()->getPackage($packageName); }
/** * Loads the given action * * @param Action|string $actionName * @param string $format the response type (e.g. html, json, ...) * @return AbstractAction */ public function loadAction($nameOrAction, $format = null) { $model = null; if ($nameOrAction instanceof Action) { $model = $nameOrAction; $actionName = $nameOrAction->getName(); } else { $actionName = $nameOrAction; } if (!isset($this->actions[$actionName])) { throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $actionName, $this->model->getName())); } if ($model === null) { $model = $this->actions[$actionName]['model']; } /* @var $action ActionSchema */ $action = $this->actions[$actionName]['action']; // check permission if (!$this->service->getFirewall()->hasActionPermission($model)) { throw new PermissionDeniedException(sprintf('Can\'t access Action (%s) in Module (%s)', $actionName, $this->model->getName())); } // check if a response is given if ($format !== null) { if (!$action->hasResponder($format)) { throw new ModuleException(sprintf('No Responder (%s) given for Action (%s) in Module (%s)', $format, $actionName, $this->model->getName())); } $responseClass = $action->getResponder($format); if (!class_exists($responseClass)) { throw new ModuleException(sprintf('Responder (%s) not found in Module (%s)', $responseClass, $this->model->getName())); } $responder = new $responseClass($this); } else { $responder = new NullResponder($this); } // gets the action class $className = $model->getClassName(); if (!class_exists($className)) { throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $className, $this->model->getName())); } $class = new $className($model, $this, $responder); // locales // ------------ $localeService = $this->getServiceContainer()->getLocaleService(); // load module l10n $file = sprintf('/%s/locales/{locale}/translations.json', $this->package->getFullName()); $localeService->loadLocaleFile($file, $class->getCanonicalName()); // // load additional l10n files // foreach ($action->getL10n() as $file) { // $file = sprintf('/%s/locales/{locale}/%s', $this->package->getFullName(), $file); // $localeService->loadLocaleFile($file, $class->getCanonicalName()); // } // // load action l10n // $file = sprintf('/%s/locales/{locale}/actions/%s', $this->package->getFullName(), $actionName); // $localeService->loadLocaleFile($file, $class->getCanonicalName()); // assets // ------------ $app = $this->getServiceContainer()->getKernel()->getApplication(); $page = $app->getPage(); // scripts foreach ($action->getScripts() as $script) { $page->addScript($script); } // styles foreach ($action->getStyles() as $style) { $page->addStyle($style); } return $class; }
/** * Checks the permission for the given module and action name * * @param string $module * @param string $action * @param User $user * @return bool */ public function hasPermission($module, $action, User $user = null) { $module = $this->service->getModuleManager()->load($module); $action = $module->getActionModel($action); return $this->hasActionPermission($action, $user); }