From b0df468c59d7c5017c140b5c9d4e0e67e7efaca6 Mon Sep 17 00:00:00 2001 From: Acrack Date: Mon, 14 Apr 2014 10:46:00 +0200 Subject: [PATCH 1/2] context --- Annotation/Bigfoot/Context.php | 13 ++ BigfootCoreBundle.php | 7 - Command/Bigfoot/ThemeInstallCommand.php | 4 +- Controller/BaseController.php | 43 ++++- Controller/CrudController.php | 28 +++- Event/FormEvent.php | 11 ++ Manager/RouteManager.php | 157 ------------------- ORM/Doctrine/Query/MySQL/Regexp.php | 47 ++++++ Resources/config/services.yml | 39 ++--- Subscriber/MenuSubscriber.php | 2 +- Utils/CommonUtils.php => Util/CommonUtil.php | 4 +- Utils/StringUtils.php => Util/StringUtil.php | 4 +- 12 files changed, 156 insertions(+), 203 deletions(-) create mode 100644 Annotation/Bigfoot/Context.php create mode 100644 Event/FormEvent.php delete mode 100644 Manager/RouteManager.php create mode 100644 ORM/Doctrine/Query/MySQL/Regexp.php rename Utils/CommonUtils.php => Util/CommonUtil.php (90%) rename Utils/StringUtils.php => Util/StringUtil.php (80%) diff --git a/Annotation/Bigfoot/Context.php b/Annotation/Bigfoot/Context.php new file mode 100644 index 0000000..26defc6 --- /dev/null +++ b/Annotation/Bigfoot/Context.php @@ -0,0 +1,13 @@ +container->get('bigfoot_core.manager.route')->addBundle($this->getName()); - } } diff --git a/Command/Bigfoot/ThemeInstallCommand.php b/Command/Bigfoot/ThemeInstallCommand.php index adceac6..105e444 100644 --- a/Command/Bigfoot/ThemeInstallCommand.php +++ b/Command/Bigfoot/ThemeInstallCommand.php @@ -10,7 +10,7 @@ use Symfony\Component\Finder\Finder; use Bigfoot\Bundle\CoreBundle\Command\BaseCommand; -use Bigfoot\Bundle\CoreBundle\Utils\CommonUtils; +use Bigfoot\Bundle\CoreBundle\Util\CommonUtil; /** * Command that places the active bigfoot theme web assets into a given directory. @@ -75,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $contentBundle = $this->getContainer()->get('kernel')->getBundle('BigfootContentBundle'); $images = $contentBundle->getPath().'/Resources/public/images'; - CommonUtils::recurseCopy($images, $targetArg.'/images'); + CommonUtil::recurseCopy($images, $targetArg.'/images'); if (is_dir($originDir = $themeBundle->getPath().'/Resources/assets')) { $targetDir = $targetArg.'/admin'; diff --git a/Controller/BaseController.php b/Controller/BaseController.php index c244dfe..e466aa6 100644 --- a/Controller/BaseController.php +++ b/Controller/BaseController.php @@ -2,13 +2,16 @@ namespace Bigfoot\Bundle\CoreBundle\Controller; -use Doctrine\ORM\EntityManager; -use Doctrine\ORM\EntityRepository; -use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Security\Core\SecurityContext; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\Translation\Translator; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityRepository; + +use Bigfoot\Bundle\CoreBundle\Event\FormEvent; /** * Base Controller @@ -106,6 +109,18 @@ protected function getThemeBundle() return $this->container->getParameter('bigfoot.theme.bundle'); } + /** + * Create form + */ + public function createForm($type, $data = null, array $options = array()) + { + $form = parent::createForm($type, $data, $options); + + $this->getEventDispatcher()->dispatch(FormEvent::CREATE, new GenericEvent($form)); + + return $form; + } + /** * Render ajax */ @@ -182,6 +197,22 @@ protected function getEventDispatcher() return $this->get('event_dispatcher'); } + /** + * Get the bigfoot context + */ + protected function getContext() + { + return $this->get('bigfoot_context'); + } + + /** + * Get the bigfoot context manager + */ + protected function getContextManager() + { + return $this->get('bigfoot_context.manager.context'); + } + /** * Get the user manager */ @@ -191,11 +222,11 @@ protected function getUserManager() } /** - * Get the menu item manager + * Get the context repository */ - protected function getMenuItemManager() + protected function getContextRepository() { - return $this->get('bigfoot_navigation.manager.menu_item'); + return $this->get('bigfoot_context.repository.context'); } /** diff --git a/Controller/CrudController.php b/Controller/CrudController.php index 76d8a6f..cb7bbee 100644 --- a/Controller/CrudController.php +++ b/Controller/CrudController.php @@ -8,9 +8,11 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\PropertyAccess\PropertyAccess; use Doctrine\ORM\Query; +use Doctrine\ORM\Query\Expr\Comparison; use Bigfoot\Bundle\CoreBundle\Controller\AdminControllerInterface; use Bigfoot\Bundle\CoreBundle\Controller\BaseController; +use Bigfoot\Bundle\CoreBundle\Event\FormEvent; use Bigfoot\Bundle\UserBundle\Entity\User; /** @@ -87,6 +89,7 @@ protected function getBundleName() $names = $this->getBundleAndEntityName(); $this->bundleName = $names['bundle']; } + return $this->bundleName; } @@ -134,6 +137,7 @@ protected function getFormType() protected function getEntityClass() { $namespace = $this->get('kernel')->getBundle($this->getBundleName())->getNamespace(); + return sprintf('\\%s\\Entity\\%s', $namespace, $this->getEntityName()); } @@ -143,6 +147,7 @@ protected function getEntityClass() protected function getEntityTypeClass() { $namespace = $this->container->get('kernel')->getBundle($this->getBundleName())->getNamespace(); + return sprintf('\\%s\\Form\\%sType', $namespace, $this->getEntityName()); } @@ -261,14 +266,18 @@ protected function getGlobalActions() */ protected function doIndex() { + $context = 'gaumont'; + $entityClass = ltrim($this->getEntityClass(), '\\'); + $query = $this - ->getRepository($this->getEntity()) - ->createQueryBuilder('e') + ->getContextRepository() + ->createContextQueryBuilder($entityClass) ->getQuery() ->setHint( Query::HINT_CUSTOM_OUTPUT_WALKER, - 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker' - ); + 'Gedmo\Translatable\Query\TreeWalker\TranslationWalker' + ) + ->getResult(); return $this->renderIndex($query); } @@ -295,6 +304,8 @@ protected function doNew(Request $request) $this->persistAndFlush($entity); + $this->postFlush($entity, 'new'); + if (!$request->isXmlHttpRequest()) { $action = $this->generateUrl($this->getRouteNameForAction('edit'), array('id' => $entity->getId())); @@ -342,6 +353,8 @@ protected function doEdit(Request $request, $id) $this->persistAndFlush($entity); + $this->postFlush($entity, 'edit'); + if (!$request->isXmlHttpRequest()) { $this->addSuccessFlash('The %entity% has been updated.'); @@ -485,4 +498,11 @@ protected function handleSuccessResponse($action, $entity = null) * @param object $entity entity */ protected function prePersist($entity, $action) {} + + /** + * Post flush entity + * + * @param object $entity entity + */ + protected function postFlush($entity, $action) {} } diff --git a/Event/FormEvent.php b/Event/FormEvent.php new file mode 100644 index 0000000..7c9b7fc --- /dev/null +++ b/Event/FormEvent.php @@ -0,0 +1,11 @@ + boolean. - * For each bundle added to the route manager, holds false by default, true if its routes already have been parsed. - * - * @var array - */ - protected $loaded; - - protected $router; - - /** - * Construct RouteManager - * - * @param kernel $kernel - * @param routeLoader $routeLoader - */ - public function __construct(AppKernel $kernel, DelegatingLoader $routeLoader, $router) - { - $this->kernel = $kernel; - $this->routeLoader = $routeLoader; - $this->bundles = array(); - $this->routes = array(); - $this->loaded = array(); - $this->router = $router; - } - - /** - * For each bundle not yet loaded, calls the sf2 RouteLoader to find all routes for which a "label" option is defined, and adds them to $this->routes. - */ - protected function loadRoutes() - { - $routes = $this->routes; - - foreach ($this->bundles as $bundle) { - if (!$this->loaded[$bundle]) { - $resource = $this->kernel->locateResource(sprintf('@%s/Controller/', $bundle)); - $routes = array_merge($routes, $this->routeLoader->load($resource)->all()); - } - } - - foreach($routes as $routeName => $route) { - if (array_key_exists('label', $route->getOptions())) { - $this->routes[$routeName] = $route; - } - } - } - - /** - * If at least one bundle is marked as not yet loaded, calls RouteManager::loadRoutes(). - * - * @return array The parsed routes. - */ - public function getRoutes() - { - if (in_array(false, $this->loaded)) { - $this->loadRoutes(); - } - - return $this->routes; - } - - /** - * @return array The parsed routes as a name => label associative array to ease use in forms. - */ - public function getArrayRoutes() - { - $tabRoutes = array(); - - foreach ($this->getRoutes() as $key => $route) { - if ($label = $route->getOption('label')) { - $tabRoutes[$key] = $label; - } - } - - return $tabRoutes; - } - - public function getArrayRouteCollection() - { - $routes = $this->router->getRouteCollection(); - $nRoutes = array(); - - foreach ($routes as $key => $route) { - $routeOptions = $route->getOptions(); - - if (isset($routeOptions['label'])) { - $nRoutes[$key] = $routeOptions['label']; - } - } - - asort($nRoutes); - - return $nRoutes; - } - - /** - * Adds a bundle in the "to be parsed" list. - * Sets $this->loaded to false for that bundle to force the parsing in subsequent calls to RouteManager::getRoutes() - * - * @param $bundleName The full bundle name to be parsed. - * @return $this - */ - public function addBundle($bundleName) - { - $this->bundles[] = $bundleName; - $this->loaded[$bundleName] = false; - - return $this; - } -} \ No newline at end of file diff --git a/ORM/Doctrine/Query/MySQL/Regexp.php b/ORM/Doctrine/Query/MySQL/Regexp.php new file mode 100644 index 0000000..f306514 --- /dev/null +++ b/ORM/Doctrine/Query/MySQL/Regexp.php @@ -0,0 +1,47 @@ +getEntityManager()->createQuery('SELECT A FROM Entity A WHERE REGEXP(A.stringField, :regexp) = 1'); +* $query->setParameter('regexp', '^[ABC]'); +* $results = $query->getArrayResult(); +*/ + +use Doctrine\ORM\Query\AST\Functions\FunctionNode; +use Doctrine\ORM\Query\Lexer; + +class Regexp extends FunctionNode +{ + public $value = null; + public $regexp = null; + + public function parse(\Doctrine\ORM\Query\Parser $parser) + { + $parser->match(Lexer::T_IDENTIFIER); + $parser->match(Lexer::T_OPEN_PARENTHESIS); + + $this->value = $parser->StringPrimary(); + $parser->match(Lexer::T_COMMA); + + $this->regexp = $parser->StringExpression(); + $parser->match(Lexer::T_CLOSE_PARENTHESIS); + } + + public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) + { + return '(' . $this->value->dispatch($sqlWalker) . ' REGEXP ' . $this->regexp->dispatch($sqlWalker) . ')'; + } +} \ No newline at end of file diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 7ce351a..9679a75 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -96,30 +96,32 @@ services: - [setContainer, ["@service_container"]] tags: - { name: bigfoot.dashboard, alias: dashboard } + # Twig + bigfoot_core.twig.locale_flags_extension: + class: Bigfoot\Bundle\CoreBundle\Twig\LocalesFlagsExtension + arguments: + - %bigfoot_core.languages.front% + tags: + - { name: twig.extension } + - # Route Manager - bigfoot_core.manager.route: - class: Bigfoot\Bundle\CoreBundle\Manager\RouteManager - arguments: - - @kernel - - @routing.loader - - @router - # Token generator + + # Token Generator bigfoot_core.generator.token: class: Bigfoot\Bundle\CoreBundle\Generator\TokenGenerator # Abstract Mailer bigfoot_core.mailer.abstract: class: Bigfoot\Bundle\CoreBundle\Mailer\AbstractMailer - abstract: true + abstract: true calls: - - [setManager, ["@doctrine.orm.entity_manager"]] - - [setMailer, ["@mailer"]] - - [setTemplating, ["@templating"]] - - [setTranslator, ["@translator"]] - - [setMailFrom, [ "%bigfoot_core.mailer.from%" ]] + - [setManager, ['@doctrine.orm.entity_manager']] + - [setMailer, ['@mailer']] + - [setTemplating, ['@templating']] + - [setTranslator, ['@translator']] + - [setMailFrom, ['%bigfoot_core.mailer.from%']] # Menu Builder bigfoot_core.menu_builder: @@ -163,13 +165,6 @@ services: bigfoot_core.subscriber.knp: class: Bigfoot\Bundle\CoreBundle\Subscriber\KnpSubscriber calls: - - [setRequest, ["@?request="]] + - [setRequest, ['@?request=']] tags: - { name: knp_paginator.subscriber } - - # Twig - bigfoot_core.twig.locale_flags_extension: - class: Bigfoot\Bundle\CoreBundle\Twig\LocalesFlagsExtension - arguments: [ %bigfoot_core.languages.front% ] - tags: - - { name: twig.extension } diff --git a/Subscriber/MenuSubscriber.php b/Subscriber/MenuSubscriber.php index 8f66ddc..8700169 100644 --- a/Subscriber/MenuSubscriber.php +++ b/Subscriber/MenuSubscriber.php @@ -4,10 +4,10 @@ use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Security\Core\SecurityContextInterface; use Doctrine\ORM\EntityManager; use Bigfoot\Bundle\CoreBundle\Event\MenuEvent; -use Symfony\Component\Security\Core\SecurityContextInterface; /** * Menu Subscriber diff --git a/Utils/CommonUtils.php b/Util/CommonUtil.php similarity index 90% rename from Utils/CommonUtils.php rename to Util/CommonUtil.php index 6eb3140..82ffaea 100644 --- a/Utils/CommonUtils.php +++ b/Util/CommonUtil.php @@ -1,8 +1,8 @@ Date: Mon, 14 Apr 2014 16:16:00 +0200 Subject: [PATCH 2/2] refacto context --- Controller/BaseController.php | 2 ++ Controller/CrudController.php | 1 - Resources/assets/js/bootstrap.confirm.js | 2 +- Theme/Section/FooterSection.php | 4 ---- Theme/Section/HeaderSection.php | 5 ----- Theme/Theme.php | 4 ---- 6 files changed, 3 insertions(+), 15 deletions(-) diff --git a/Controller/BaseController.php b/Controller/BaseController.php index e466aa6..d0dfe4c 100644 --- a/Controller/BaseController.php +++ b/Controller/BaseController.php @@ -2,6 +2,7 @@ namespace Bigfoot\Bundle\CoreBundle\Controller; +use Bigfoot\Bundle\ContextBundle\Entity\ContextRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Security\Core\SecurityContext; @@ -223,6 +224,7 @@ protected function getUserManager() /** * Get the context repository + * @return ContextRepository */ protected function getContextRepository() { diff --git a/Controller/CrudController.php b/Controller/CrudController.php index cb7bbee..cbab55c 100644 --- a/Controller/CrudController.php +++ b/Controller/CrudController.php @@ -266,7 +266,6 @@ protected function getGlobalActions() */ protected function doIndex() { - $context = 'gaumont'; $entityClass = ltrim($this->getEntityClass(), '\\'); $query = $this diff --git a/Resources/assets/js/bootstrap.confirm.js b/Resources/assets/js/bootstrap.confirm.js index 8bf9fc0..6af5aa3 100644 --- a/Resources/assets/js/bootstrap.confirm.js +++ b/Resources/assets/js/bootstrap.confirm.js @@ -8,7 +8,7 @@ { var confirmLink = $(this); modalEvent.preventDefault(); - var targetData = $(modalEvent.target).data();console.log(targetData); + var targetData = $(modalEvent.target).data(); var modal = Twig.render(confirmModal, targetData); confirmContainer.html(modal); diff --git a/Theme/Section/FooterSection.php b/Theme/Section/FooterSection.php index 4c6a6f3..80fc0e3 100644 --- a/Theme/Section/FooterSection.php +++ b/Theme/Section/FooterSection.php @@ -21,9 +21,5 @@ public function getName() */ protected function setDefaultParameters() { - $themeValues = $this->container->getParameter('bigfoot.theme.values'); - $this->parameters = array( - 'title' => $themeValues['provided_by'], - ); } } diff --git a/Theme/Section/HeaderSection.php b/Theme/Section/HeaderSection.php index 3ee32da..c500700 100644 --- a/Theme/Section/HeaderSection.php +++ b/Theme/Section/HeaderSection.php @@ -21,10 +21,5 @@ public function getName() */ protected function setDefaultParameters() { - $themeValues = $this->container->getParameter('bigfoot.theme.values'); - $this->parameters = array( - 'title' => $themeValues['title'], - 'subtitle' => $themeValues['subtitle'], - ); } } diff --git a/Theme/Theme.php b/Theme/Theme.php index 149cc8e..ac403c0 100644 --- a/Theme/Theme.php +++ b/Theme/Theme.php @@ -45,10 +45,6 @@ class Theme implements ArrayAccess */ public function __construct(Container $container, Section\ToolbarSection $toolbar, Section\HeaderSection $header, Section\SidebarSection $sidebar, Section\PageHeaderSection $pageHeader, Section\PageContentSection $pageContent, Section\FooterSection $footer) { - $themeValues = $container->getParameter('bigfoot.theme.values'); - $header->setParameter('title', $themeValues['title']); - $header->setParameter('subtitle', $themeValues['subtitle']); - $sections = array(); $sections[] = $toolbar; $sections[] = $header;