1. Go to this page and download the library: Download sofascore/purgatory library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sofascore / purgatory example snippets
use App\Kernel;
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
t['APP_ENV'], (bool) $context['APP_DEBUG']));
};
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @ORM\Table(name="post1")
*/
class Post
{
/**
* @ORM\Id()
* @ORM\Column(type="integer")
* @ORM\GeneratedValue()
*/
public $id;
/**
* @ORM\Column(type="string")
*/
public $title;
/**
* @ORM\Column(type="string")
*/
public $content;
}
namespace App\Controller;
use App\Entity\Post;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/post")
*/
class PostController extends AbstractController
{
public function __construct(private EntityManagerInterface $entityManager)
{
}
/**
* @Route("/{postId<\d+>}", methods={"GET"})
*/
public function detailsAction(int $postId)
{
/** @var Post $post */
$post = $this->entityManager->getRepository(Post::class)->find($postId);
if (null === $post) {
return new Response(status: 404);
}
$r = new Response(json_encode(['title' => $post->title, 'content'=>$post->content]), 200, []);
$r->setSharedMaxAge(3600);
$r->setMaxAge(3600);
return $r;
}
}
use Sofascore\PurgatoryBundle\Annotation\PurgeOn;
/**
* @Route("/{postId<\d+>}", methods={"GET"})
* @PurgeOn(Post::class, parameters={"postId":"id"}, properties={"title", "content"}, if="obj.title !== null")
*/
public function indexAction(int $id) //...
namespace App\Service;
use GuzzleHttp\Client;
use Sofascore\PurgatoryBundle\Purger\PurgerInterface;
class VarnishPurger implements PurgerInterface
{
private Client $client;
public function __construct()
{
$this->client = new Client();
}
public function purge(iterable $urls): void
{
foreach ($urls as $url) {
$this->client->request('PURGE', 'http://varnish_host' . $url);
}
}
}
/**
* @Route("/{postId<\d+>}", methods={"GET"})
* @PurgeOn(Post::class, parameters={"postId":"id"})
*/
public function detailsAction(int $postId) {
/**
* @Route("/featured", methods={"GET"})
* @PurgeOn(Post::class, properties={"featured"})
*/
public function featuredAction() {
/**
* @Route("/popular", methods={"GET"})
* @PurgeOn(Post::class, if="obj.upvotes > 3000")
*/
public function popularAction(int $postId) {