1
0
Fork 0
mirror of https://github.com/archtechx/laravel-tips.git synced 2025-12-12 05:14:04 +00:00

initial commit

This commit is contained in:
Samuel Štancl 2021-04-06 18:27:18 +02:00
commit 1beb9dd34c
281 changed files with 56773 additions and 0 deletions

View file

@ -0,0 +1,78 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Routing\RedirectController;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class GenerateHtml extends Command
{
protected $signature = 'html:generate {outdir} {rootUrl}';
protected $description = 'Generate the static HTML.';
public function __construct(
public Router $router,
public UrlGenerator $url,
) {
parent::__construct();
}
public function handle()
{
$outdir = realpath($this->argument('outdir'));
$this->url->forceRootUrl($this->argument('rootUrl'));
File::copyDirectory(public_path(), $outdir);
collect($this->router->getRoutes()->get())
->filter(fn (Route $route) => in_array('static', $route->gatherMiddleware()))
->values()
->each(function (Route $route) use ($outdir) {
if (! count($route->parameterNames())) {
if ($route->getActionName() === '\\' . RedirectController::class) {
// $action = $this->router->match('get', $route->defaults['destination'])->getAction();
// Redirects don't work yet
$action = $route->getAction();
} else {
$action = $route->getAction();
}
if (! is_dir($path = $outdir . '/' . $route->uri())) {
mkdir($path, 0777, true);
}
File::put($outdir . '/' . $route->uri() . '/index.html', $this->render($action['uses']()));
} else {
$model = 'App\\Models\\' . ucfirst($parameter = $route->parameterNames()[0]);
$root = (string) Str::of($outdir . '/' . $route->uri())->before("{{$parameter}}");
foreach ($model::cursor() as $instance) {
if (! is_dir($path = $root . $instance->getRouteKey())) {
mkdir($path, 0777, true);
}
File::put($path . '/index.html', $this->render($route->getAction()['uses']($instance)));
}
}
});
return 0;
}
protected function render ($response) {
if ($response instanceof Renderable) {
return $response->render();
}
return $response;
}
}

41
app/Console/Kernel.php Normal file
View file

@ -0,0 +1,41 @@
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
//
});
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

68
app/Http/Kernel.php Normal file
View file

@ -0,0 +1,68 @@
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'static' => [],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}

View file

@ -0,0 +1,21 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
//
];
}

View file

@ -0,0 +1,17 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Middleware;
class PreventRequestsDuringMaintenance extends Middleware
{
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
* @var array
*/
protected $except = [
//
];
}

View file

@ -0,0 +1,32 @@
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null ...$guards
* @return mixed
*/
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
}
return $next($request);
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
class TrimStrings extends Middleware
{
/**
* The names of the attributes that should not be trimmed.
*
* @var array
*/
protected $except = [
'current_password',
'password',
'password_confirmation',
];
}

View file

@ -0,0 +1,20 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Middleware\TrustHosts as Middleware;
class TrustHosts extends Middleware
{
/**
* Get the host patterns that should be trusted.
*
* @return array
*/
public function hosts()
{
return [
$this->allSubdomainsOfApplicationUrl(),
];
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace App\Http\Middleware;
use Fideloper\Proxy\TrustProxies as Middleware;
use Illuminate\Http\Request;
class TrustProxies extends Middleware
{
/**
* The trusted proxies for this application.
*
* @var array|string|null
*/
protected $proxies;
/**
* The headers that should be used to detect proxies.
*
* @var int
*/
protected $headers = Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO | Request::HEADER_X_FORWARDED_AWS_ELB;
}

View file

@ -0,0 +1,17 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
//
];
}

14
app/MarkdownCompiler.php Normal file
View file

@ -0,0 +1,14 @@
<?php
namespace App;
use League\CommonMark\GithubFlavoredMarkdownConverter;
use Illuminate\Support\Str;
class MarkdownCompiler
{
public static function compileMarkdownString(string $expression): string
{
return (new GithubFlavoredMarkdownConverter())->convertToHtml($expression);
}
}

42
app/Models/Author.php Normal file
View file

@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Orbit\Concerns\Orbital;
/**
* @property string $username
* @property string $avatar
* @property string $name Full name.
* @property-read string $profile_url
*/
class Author extends Model
{
use Orbital;
public $timestamps = false;
public static function schema(Blueprint $table)
{
$table->string('username')->primary();
$table->string('avatar');
$table->string('name');
}
public function getProfileUrlAttribute(): string
{
return 'https://twitter.com/' . $this->username;
}
public function getKeyName()
{
return 'username';
}
public function getIncrementing()
{
return false;
}
}

57
app/Models/Thread.php Normal file
View file

@ -0,0 +1,57 @@
<?php
namespace App\Models;
use App\Twitter\Tweet;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Schema\Blueprint;
use Orbit\Concerns\Orbital;
class Thread extends Model
{
use Orbital;
public $timestamps = false;
public static function schema(Blueprint $table)
{
$table->string('slug')->unique();
$table->string('title');
$table->string('tweet_id')->nullable();
$table->foreignId('author_username')->constrained('authors', 'username');
$table->text('content');
$table->timestamp('created_at');
}
public static function booted()
{
static::creating(fn (self $model) => $model->created_at ??= now());
}
public function tips(): HasMany
{
return $this->hasMany(Tip::class);
}
public function author(): BelongsTo
{
return $this->belongsTo(Author::class, 'author_username', 'username');
}
public function getTweetUrlAttribute(): string|null
{
return "https://twitter.com/{$this->author_username}/status/{$this->tweet_id}";
}
public function getKeyName()
{
return 'slug';
}
public function getIncrementing()
{
return false;
}
}

103
app/Models/Tip.php Normal file
View file

@ -0,0 +1,103 @@
<?php
namespace App\Models;
use App\Twitter\Tweet;
use App\Twitter\TwitterImage;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Schema\Blueprint;
use Orbit\Concerns\Orbital;
use Illuminate\Support\Str;
/**
* @property string $slug
* @property string $title
* @property string $content
* @property string $tweet_id
* @property string $author_username
* @property string|null $thread_id
* @property string[] $images
* @property-read Author $author
* @property-read Thread $thread
* @property-read \Illuminate\Eloquent\Collection|Image[] $images
* @property-read string $tweet_url
*/
class Tip extends Model
{
use HasFactory, Orbital;
public $timestamps = false;
public $casts = [
'images' => 'array',
];
public static function schema(Blueprint $table)
{
$table->string('slug')->unique();
$table->string('title');
$table->string('tweet_id');
$table->foreignId('thread_slug')->constrained('threads', 'slug')->nullable();
$table->foreignId('author_username')->constrained('authors', 'username');
$table->json('images')->default('[]');
$table->timestamp('created_at');
}
public function thread(): BelongsTo
{
return $this->belongsTo(Thread::class, 'thread_slug', 'slug');
}
public function author(): BelongsTo
{
return $this->belongsTo(Author::class, 'author_username', 'username');
}
public function getTweetUrlAttribute(): string|null
{
return "https://twitter.com/{$this->author_username}/status/{$this->tweet_id}";
}
/** @return TwitterImage[] */
public function images(): array
{
return array_map(fn (string $url) => new TwitterImage($url), $this->images);
}
public static function fromTweet(Tweet $tweet, string $threadSlug = null): static
{
return new static([
'title' => (string) Str::of(Str::of($tweet->text)->explode("\n")->first())->rtrim('.')->replaceMatches('/^([^a-zA-Z]*)/', ''), // remove any non-ascii characters from the beginning
'content' => (string) Str::of($tweet->text)->explode("\n")->skip(1)->join("\n"),
'tweet_id' => $tweet->id,
'thread_slug' => $threadSlug ?? Thread::firstWhere('tweet_id', $tweet->threadId)->slug,
'author_username' => Author::firstOrCreate([
'username' => $tweet->author->username,
'name' => $tweet->author->name,
'avatar' => $tweet->author->profile_image_url,
])->username,
'images' => array_map(fn (TwitterImage $image) => $image->url, $tweet->images),
]);
}
public static function booted()
{
static::creating(function (self $model) {
$model->created_at ??= now();
$model->slug ??= Str::slug($model->title);
});
}
public function getKeyName()
{
return 'slug';
}
public function getIncrementing()
{
return false;
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace App\Providers;
use App\MarkdownCompiler;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use League\CommonMark\GithubFlavoredMarkdownConverter;
use Illuminate\Support\Str;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Model::unguard();
Blade::directive('markdown', function (string $expression = '') {
if ($expression) {
return MarkdownCompiler::compileMarkdownString($expression);
}
return '<?php $__markdownOldBuffer = ob_get_clean(); ob_start(); ?>';
});
Blade::directive('endmarkdown', function () {
return '<?php $__markdownString = ob_get_clean(); ob_start(); echo $__markdownOldBuffer; unset($__markdownOldBuffer); echo \App\MarkdownCompiler::compileMarkdownString($__markdownString); unset($__markdownString); ?>';
});
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// 'App\Models\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
//
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\ServiceProvider;
class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Broadcast::routes();
require base_path('routes/channels.php');
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
//
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/home';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* @var string|null
*/
// protected $namespace = 'App\\Http\\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
}

60
app/Twitter/Tweet.php Normal file
View file

@ -0,0 +1,60 @@
<?php
namespace App\Twitter;
use Illuminate\Support\Facades\Http;
/**
* @property TwitterImage[] $images
*/
class Tweet
{
public function __construct(
public string $id,
public string $text,
public TwitterUser $author,
public string|null $threadId = null,
public array $images = [],
) {}
/** Fetch a tweet. */
public static function fetch(string $id): Tweet
{
$response = Http::asJson()
->withToken(config('services.twitter.token'))
->get('https://api.twitter.com/2/tweets/' . $id, [
'media.fields' => 'url',
'tweet.fields' => 'conversation_id,entities',
'user.fields' => 'profile_image_url',
'expansions' => 'attachments.media_keys,author_id',
]);
$text = $response->json('data.text');
$author = $response->json('includes.users.*')[0];
$links = $response->json('data.entities.urls', []);
foreach ($links as $link) {
// Image links are removed
if (str_starts_with($link['display_url'], 'pic.twitter.com')) {
$text = str_replace($link['url'], '', $text);
}
// Other links are converted to the full form
$text = str_replace($link['url'], $link['expanded_url'], $text);
}
return new static(
$id,
trim($text),
new TwitterUser(
$author['id'],
$author['name'],
$author['username'],
$author['profile_image_url'],
),
$response->json('data.conversation_id'),
array_map(fn (string $url) => new TwitterImage($url), $response->json('includes.media.*.url', [])),
);
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace App\Twitter;
/**
* @method string tiny() Get the URL for the tiny size version of the image.
* @method string small() Get the URL for the small size version of the image.
* @method string medium() Get the URL for the medium size version of the image.
* @method string large() Get the URL for the large size version of the image.
*/
class TwitterImage
{
public function __construct(
public string $url,
) {}
public function __call($size, $args): string
{
return $this->url . "?name={$size}";
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace App\Twitter;
class TwitterUser
{
public function __construct(
public string $id,
public string $name,
public string $username,
public string $profile_image_url,
) {}
}