Skip to content

Instantly share code, notes, and snippets.

View mansha99's full-sized avatar

Manish Sharma mansha99

View GitHub Profile
<?php
class VehicleServiceBinder
{
//Singleton Pattern
private static $instance;
public static function getVehicleService()
{
if (self::$instance == null) {
self::$instance = new FooVehicleService();
@mansha99
mansha99 / FooVehicleService.php
Created August 11, 2023 17:20
Implementing Class
<?php
namespace App\Services;
class FooVehicleService implements IVehicleService
{
public function find($vehicleNumber)
{
//logic to find Vehicle, probably in database
}
@mansha99
mansha99 / IVehicleService.php
Created August 11, 2023 17:19
Interface for loose coupling
<?php
interface IVehicleService
{
public function find($vehicleNumber);
}
@mansha99
mansha99 / VehicleSearchController.php
Created August 11, 2023 17:16
Client Component directly interacting with Server Component: Bad idea
<?php
namespace App\Http\Controllers;
class VehicleSearchController extends Controller
{
public function searchVehicle(VehicleSearchRequest $request)
{
$data = $request->validated();
//Instantiate (FooVehicleService is directly EXPOSED)
@mansha99
mansha99 / ServiceComponent.php
Created August 11, 2023 17:14
Creating Service Component
<?php
namespace App\Services;
class FooVehicleService
{
public function find($vehicleNumber)
{
//logic to find Vehicle, probably in database
}
@mansha99
mansha99 / repo-component.tsx
Created August 6, 2023 04:32
NextJS Component to illustrate Streaming
async function getDataFromServer(repo: string) {
//2 seconds delay, just for explaination
var n = 3 + Math.floor(Math.random() * 5);
await timeout(n * 1000);
const res = await fetch("https://api.github.com/users/" + repo, {
cache: "force-cache",
});
if (!res.ok) {
throw new Error("Failed to fetch data : " + res.status);
}
@mansha99
mansha99 / repo-component-loading.tsx
Created August 6, 2023 04:31
Loading component for Suspense
export default function RepoComponentLoading() {
return (
<main className="p-5 border-red-100 ">
<table className="table-fixed">
<tbody>
<tr>
<th className="border border-slate-300 p-3">Login</th>
<td className="border border-slate-300 p-3"> ..... </td>
</tr>
<tr>
@mansha99
mansha99 / page.tsx
Created August 6, 2023 04:25
Streaming in NextJS : Wrapping Components inside Suspense
import RepoComponent from "@/components/repo-component";
import RepoComponentLoading from "@/components/repo-component-loading";
import { Suspense } from "react";
export default function Home() {
return (
<main className="p-10 ">
<div className="flex flex-row">
<Suspense fallback={<RepoComponentLoading />}>
<RepoComponent slug="twitter" />
@mansha99
mansha99 / calculator.blade.php
Created August 5, 2023 10:29
Using class based syntax to create Laravel Livewire Volt Component with Form validation
<?php
use Livewire\Volt\Component;
use Livewire\Attributes\Rule;
new class extends Component {
#[Rule(['required', 'numeric'])]
public $inputvalue = 0;
public $result = null;
public function updateResult()
{
@mansha99
mansha99 / counter.blade.php
Created August 5, 2023 10:17
Using class based syntax to create Laravel Livewire Volt Component
<?php
use Livewire\Volt\Component;
use Livewire\Attributes\Rule;
new class extends Component {
public $count = 0;
public function decrement()
{
$this->count--;
}
public function increment()