Skip to content

Commit

Permalink
Merge pull request #17 from tattali/deprecations
Browse files Browse the repository at this point in the history
fix deprecations
  • Loading branch information
tattali committed Oct 20, 2019
2 parents 6c8945d + cc1a03a commit a4378ad
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 69 deletions.
21 changes: 5 additions & 16 deletions .editorconfig
Expand Up @@ -15,10 +15,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.feature]
indent_style = space
indent_size = 2

[*.json]
indent_style = space
indent_size = 2
Expand All @@ -34,14 +30,15 @@ indent_size = 4
indent_style = space
indent_size = 4

[*.yml]
[*.yaml]
indent_style = space
indent_size = 4
trim_trailing_whitespace = false

[.circleci/config.yml]
[*.yml]
indent_style = space
indent_size = 2
indent_size = 4
trim_trailing_whitespace = false

[.gitmodules]
indent_style = tab
Expand All @@ -50,22 +47,14 @@ indent_style = tab
indent_style = space
indent_size = 4

[.styleci.yml]
[.php_cs.xml.dist]
indent_style = space
indent_size = 2

[.travis.yml]
indent_style = space
indent_size = 2

[appveyor.yml]
indent_style = space
indent_size = 2

[behat.yml]
indent_style = space
indent_size = 2

[composer.json]
indent_style = space
indent_size = 4
Expand Down
35 changes: 21 additions & 14 deletions README.md
Expand Up @@ -28,7 +28,7 @@ The source of the documentation is stored in the `src/Resources/doc/` folder in
### Installation

1. [Download CalendarBundle using composer](#1-download-calendarbundle-using-composer)
2. [Create the listener](#2-create-the-listener)
2. [Create the subscriber](#2-create-the-subscriber)
3. [Add styles and scripts in your template](#3-add-styles-and-scripts-in-your-template)

#### 1. Download CalendarBundle using composer
Expand All @@ -45,42 +45,49 @@ calendar:
resource: "@CalendarBundle/Resources/config/routing.yaml"
```

#### 2. Create the listener
You need to create a listener class to load your data into the calendar and register it as a service.
#### 2. Create the subscriber
You need to create a subscriber class to load your data into the calendar.

This listener must be called when the event `calendar.set_data` is launched.
This subscriber must be registered only if autoconfigure is false.
```yaml
# config/services.yaml
services:
# ...

App\EventListener\CalendarListener:
tags:
- { name: 'kernel.event_listener', event: 'calendar.set_data', method: load }
App\EventSubscriber\CalendarSubscriber:
```

Then, create the listener class to fill the calendar
Then, create the subscriber class to fill the calendar

See the [doctrine listener example](src/Resources/doc/doctrine-crud.md#full-listener)
See the [doctrine subscriber example](src/Resources/doc/doctrine-crud.md#full-subscriber)

```php
// src/EventListener/CalendarListener.php
// src/EventSubscriber/CalendarSubscriber.php
<?php

namespace App\EventListener;
namespace App\EventSubscriber;

use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CalendarListener
class CalendarSubscriber implements EventSubscriberInterface
{
public function load(CalendarEvent $calendar)
public static function getSubscribedEvents()
{
return [
CalendarEvents::SET_DATA => 'onCalendarSetData',
];
}

public function onCalendarSetData(CalendarEvent $calendar)
{
$start = $calendar->getStart();
$end = $calendar->getEnd();
$filters = $calendar->getFilters();

// You may want to make a custom query to fill the calendar
// You may want to make a custom query from your database to fill the calendar

$calendar->addEvent(new Event(
'Event 1',
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Expand Up @@ -2,7 +2,12 @@
"name": "tattali/calendar-bundle",
"type": "symfony-bundle",
"description": "Provides event calendar for your Symfony 4 project. Compatible with Doctrine ORM & ODM, and API like Google Calendar.",
"keywords": ["fullcalendar", "calendar", "symfony", "symfony calendar"],
"keywords": [
"fullcalendar",
"calendar",
"symfony",
"symfony calendar"
],
"homepage": "https://github.com/tattali/CalendarBundle",
"license": "MIT",
"authors": [
Expand Down
5 changes: 3 additions & 2 deletions phpcs.xml.dist 100644 → 100755
Expand Up @@ -3,7 +3,6 @@
<rule ref="PSR1.Classes.ClassDeclaration"/>
<rule ref="PSR1.Files.SideEffects"/>
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
<rule ref="Generic.CodeAnalysis.EmptyStatement"/>
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
<rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
Expand All @@ -13,12 +12,14 @@
<rule ref="Generic.NamingConventions.ConstructorName"/>
<rule ref="Squiz.Scope.MethodScope"/>
<rule ref="PSR2.Classes.PropertyDeclaration"/>
<rule ref="Squiz.PHP.CommentedOutCode"/>
<rule ref="Squiz.PHP.GlobalKeyword"/>
<rule ref="Squiz.Functions.FunctionDuplicateArgument"/>
<rule ref="Squiz.Operators.ValidLogicalOperators"/>
<rule ref="PSR2.Classes.ClassDeclaration"/>
<rule ref="PSR2.Methods.MethodDeclaration"/>
<rule ref="PSR2.Namespaces.UseDeclaration"/>
<rule ref="PSR2.ControlStructures.SwitchDeclaration"/>

<file>src/</file>
<file>tests/</file>
</ruleset>
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Expand Up @@ -9,7 +9,7 @@
<php>
<ini name="error_reporting" value="-1" />
<ini name="memory_limit" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak_vendors" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[total]=0" />
</php>

<testsuites>
Expand Down
16 changes: 13 additions & 3 deletions src/Controller/CalendarController.php
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;

class CalendarController extends AbstractController
{
Expand Down Expand Up @@ -39,9 +40,9 @@ public function loadAction(Request $request): Response
$filters = $request->get('filters', '{}');
$filters = \is_array($filters) ? $filters : json_decode($filters, true);

$event = $this->eventDispatcher->dispatch(
CalendarEvents::SET_DATA,
new CalendarEvent($start, $end, $filters)
$event = $this->dispatchWithBC(
new CalendarEvent($start, $end, $filters),
CalendarEvents::SET_DATA
);
$content = $this->serializer->serialize($event->getEvents());

Expand All @@ -52,4 +53,13 @@ public function loadAction(Request $request): Response

return $response;
}

public function dispatchWithBC($event, string $eventName)
{
if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
return $this->eventDispatcher->dispatch($event, $eventName);
}

return $this->eventDispatcher->dispatch($eventName, $event);
}
}
7 changes: 6 additions & 1 deletion src/Event/CalendarEvent.php
Expand Up @@ -5,9 +5,14 @@
namespace CalendarBundle\Event;

use CalendarBundle\Entity\Event;
use CalendarBundle\Event\Event as BaseEvent;
use DateTimeInterface;
use Symfony\Component\EventDispatcher\Event as BaseEvent;

/**
* This event is triggered before the serialization of the events.
*
* This event allows you to fill the calendar with your data.
*/
class CalendarEvent extends BaseEvent
{
/**
Expand Down
22 changes: 22 additions & 0 deletions src/Event/Event.php
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace CalendarBundle\Event;

use Symfony\Component\EventDispatcher\Event as BaseEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Contracts\EventDispatcher\Event as ContractsBaseEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

// phpcs:disable
if (is_subclass_of(EventDispatcher::class, EventDispatcherInterface::class)) {
class Event extends ContractsBaseEvent
{
}
} else {
class Event extends BaseEvent
{
}
}
// phpcs:enable
55 changes: 33 additions & 22 deletions src/Resources/doc/doctrine-crud.md
Expand Up @@ -7,7 +7,7 @@ This example allow you to create, update, delete & show events with `CalendarBun
1. [Download CalendarBundle using composer](#1-download-calendarbundle-using-composer)
2. [Create the entity](#2-create-the-entity)
3. [Create the CRUD](#3-create-the-crud)
4. [Use an event listener to connect all of this together](#4-use-an-event-listener-to-connect-all-of-this-together)
4. [Use an event subscriber to connect all of this together](#4-use-an-event-subscriber-to-connect-all-of-this-together)
5. [Display your calendar](#5-display-your-calendar)

### 1. Download CalendarBundle using composer
Expand Down Expand Up @@ -186,35 +186,34 @@ class BookingController extends AbstractController
}
```

### 4. Use an event listener to connect all of this together
### 4. Use an event subscriber to connect all of this together

Register the listener as a service to listen `calendar.set_data` event
This subscriber must be registered only if autoconfigure is false.
```yaml
# config/services.yaml
services:
# ...

App\EventListener\CalendarListener:
tags:
- { name: 'kernel.event_listener', event: 'calendar.set_data', method: load }
App\EventSubscriber\CalendarSubscriber:
```

We now have to link the CRUD to the calendar by adding the `booking_show` route in each events

[TL;DR](#full-listener)
[TL;DR](#full-subscriber)

To do this create a listener with access to the router component and your entity repository
To do this create a subscriber with access to the router component and your entity repository
```php
// src/EventListener/CalendarListener.php
// src/EventSubscriber/CalendarSubscriber.php
<?php

namespace App\EventListener;
namespace App\EventSubscriber;

// ...
use App\Repository\BookingRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class CalendarListener
class CalendarSubscriber implements EventSubscriberInterface
{
private $bookingRepository;
private $router;
Expand All @@ -241,23 +240,24 @@ $bookingEvent->addOption(
);
```

#### Full listener
#### Full subscriber

Full listener with `Booking` entity. Modify it to fit your needs.
Full subscriber with `Booking` entity. Modify it to fit your needs.

```php
// src/EventListener/CalendarListener.php
// src/EventSubscriber/CalendarSubscriber.php
<?php

namespace App\EventListener;
namespace App\EventSubscriber;

use App\Entity\Booking;
use App\Repository\BookingRepository;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class CalendarListener
class CalendarSubscriber implements EventSubscriberInterface
{
private $bookingRepository;
private $router;
Expand All @@ -270,7 +270,14 @@ class CalendarListener
$this->router = $router;
}

public function load(CalendarEvent $calendar): void
public static function getSubscribedEvents()
{
return [
CalendarEvents::SET_DATA => 'onCalendarSetData',
];
}

public function onCalendarSetData(CalendarEvent $calendar)
{
$start = $calendar->getStart();
$end = $calendar->getEnd();
Expand All @@ -280,7 +287,7 @@ class CalendarListener
// Change booking.beginAt by your start date property
$bookings = $this->bookingRepository
->createQueryBuilder('booking')
->where('booking.beginAt BETWEEN :start and :end')
->where('booking.beginAt BETWEEN :start and :end OR booking.endAt BETWEEN :start and :end')
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'))
->getQuery()
Expand Down Expand Up @@ -325,16 +332,19 @@ class CalendarListener
Then create the calendar template

add a link to the `booking_new` form

```twig
<a href="{{ path('booking_new') }}">Create new booking</a>
```

and include the `calendar-holder`

```twig
{% include '@Calendar/calendar.html' %}
```

Full template:

```twig
{# templates/booking/calendar.html.twig #}
{% extends 'base.html.twig' %}
Expand Down Expand Up @@ -389,13 +399,14 @@ Full template:
</script>
{% endblock %}
```
* Now visit: http://localhost:8000/booking/calendar

* Now visit: <http://localhost:8000/booking/calendar>

* In the calendar when you click on an event it call the `show()` action that should contains an edit and delete link

* And when you create a new `Booking` (or your custom entity name) it appear on the calendar

* If you have created a custom entity don't forget to modify the listener:
* If you have created a custom entity don't forget to modify the subscriber:
- Replace all `Booking` or `booking` by your custom entity name
- In the query near the `where` modify `beginAt` to your custom start date property
- Also when you create each `Event` in the `foreach` modify the getters to fit with your entity
Expand Down

0 comments on commit a4378ad

Please sign in to comment.