You’ve seen Kris’ open source libraries, but how does he tackle coding out an application? Walk through green fields with a Symfony expert as he takes his latest “next big thing” idea from the first line of code to a functional prototype. Learn design patterns and principles to guide your way in organizing your own code and take home some practical examples to kickstart your next project.
42. public function __clone()
{
$this->id = (string) new MongoId();
$this->createdAt = new DateTime();
$this->widgets = new ArrayCollection(
$this->widgets->toArray()
);
}
43. public function __construct()
{
$this->id = (string) new MongoId();
$this->createdAt = new DateTime();
$this->widgets = new ArrayCollection();
}
public function __clone()
{
$this->id = (string) new MongoId();
$this->createdAt = new DateTime();
$this->widgets = new ArrayCollection(
$this->widgets->toArray()
);
}
75. public function onFlush(OnFlushEventArgs $event)
{
$dm = $event->getDocumentManager();
$uow = $dm->getUnitOfWork();
foreach ($uow->getIdentityMap() as $class => $docs) {
if (self::checkClass('OptiGrabModelUser', $class)) {
foreach ($docs as $doc) {
$this->processUserFlush($dm, $doc);
}
} elseif (self::checkClass('OptiGrabModelWidget', $class)) {
foreach ($docs as $doc) {
$this->processWidgetFlush($dm, $doc);
}
}
}
}
76. private function processUserFlush(DocumentManager $dm, User $user)
{
$uow = $dm->getUnitOfWork();
$meta = $dm->getClassMetadata('Model:User');
$changes = $uow->getDocumentChangeSet($user);
if (isset($changes['id'][1])) {
$this->dispatcher->dispatch(UserEvents::CREATE, new UserEvent($dm, $user));
}
if (isset($changes['usernameCanonical'][0]) && null !== $changes['usernameCanonical'][0]) {
$this->dispatcher->dispatch(UserEvents::USERNAME_CHANGE, new UserEvent($dm, $user));
}
if ($followedUsers = $meta->getFieldValue($user, 'followedUsers')) {
foreach ($followedUsers->getInsertDiff() as $otherUser) {
$this->dispatcher->dispatch(
UserEvents::FOLLOW_USER,
new UserUserEvent($dm, $user, $otherUser)
);
}
foreach ($followedUsers->getDeleteDiff() as $otherUser) {
// ...
}
}
}
77. /** @DIObserve("user.create") */
public function onUserCreate(UserEvent $event)
{
$user = $event->getUser();
$activity = new Activity();
$activity->setActor($user);
$activity->setVerb('register');
$activity->setCreatedAt($user->getCreatedAt());
$this->dm->persist($activity);
}
78. /** @DIObserve("user.create") */
public function onUserCreate(UserEvent $event)
{
$dm = $event->getDocumentManager();
$user = $event->getUser();
$widget = new Widget();
$widget->setUser($user);
$dm->persist($widget);
// manually notify the event
$event->getDispatcher()->dispatch(
WidgetEvents::CREATE,
new WidgetEvent($dm, $widget)
);
}
79. /** @DIObserve("user.follow_user") */
public function onFollowUser(UserUserEvent $event)
{
$event->getUser()
->getStats()
->incrementFollowedUsers(1);
$event->getOtherUser()
->getStats()
->incrementFollowers(1);
}
80. Two event classes per model
• @MainBundleUserEvents: encapsulates event name constants
such as UserEvents::CREATE and
UserEvents::CHANGE_USERNAME
• @MainBundleEventUserEvent: base event object, accepts
$dm and $user arguments
• @MainBundleWidgetEvents…
• @MainBundleEventWidgetEvent…
81. $event = new UserEvent($dm, $user);
$dispatcher->dispatch(UserEvents::CREATE, $event);