Laravel AT Protocol Client (alpha & unstable)
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add make:atp-client and make:atp-request generator commands

+296
+150
src/Console/MakeAtpClientCommand.php
··· 1 + <?php 2 + 3 + namespace SocialDept\AtpClient\Console; 4 + 5 + use Illuminate\Console\Command; 6 + use Illuminate\Filesystem\Filesystem; 7 + use Illuminate\Support\Str; 8 + 9 + class MakeAtpClientCommand extends Command 10 + { 11 + protected $signature = 'make:atp-client 12 + {name : The name of the client class} 13 + {--public : Generate a public client extension instead of authenticated} 14 + {--force : Overwrite existing file}'; 15 + 16 + protected $description = 'Create a new ATP domain client extension'; 17 + 18 + public function __construct(protected Filesystem $files) 19 + { 20 + parent::__construct(); 21 + } 22 + 23 + public function handle(): int 24 + { 25 + $name = $this->argument('name'); 26 + $isPublic = $this->option('public'); 27 + 28 + if (! Str::endsWith($name, 'Client')) { 29 + $name .= 'Client'; 30 + } 31 + 32 + $path = $this->getPath($name); 33 + 34 + if ($this->files->exists($path) && ! $this->option('force')) { 35 + $this->components->error("Client [{$name}] already exists!"); 36 + 37 + return self::FAILURE; 38 + } 39 + 40 + $this->makeDirectory($path); 41 + 42 + $stub = $isPublic ? $this->getPublicStub() : $this->getStub(); 43 + $content = $this->populateStub($stub, $name); 44 + 45 + $this->files->put($path, $content); 46 + 47 + $this->components->info("Client [{$path}] created successfully."); 48 + 49 + $this->outputRegistrationHint($name, $isPublic); 50 + 51 + return self::SUCCESS; 52 + } 53 + 54 + protected function getPath(string $name): string 55 + { 56 + $basePath = config('client.generators.client_path', 'app/Services/Clients'); 57 + 58 + return base_path($basePath.'/'.$name.'.php'); 59 + } 60 + 61 + protected function makeDirectory(string $path): void 62 + { 63 + if (! $this->files->isDirectory(dirname($path))) { 64 + $this->files->makeDirectory(dirname($path), 0755, true); 65 + } 66 + } 67 + 68 + protected function getNamespace(): string 69 + { 70 + $basePath = config('client.generators.client_path', 'app/Services/Clients'); 71 + 72 + return Str::of($basePath) 73 + ->replace('/', '\\') 74 + ->ucfirst() 75 + ->replace('App', 'App') 76 + ->toString(); 77 + } 78 + 79 + protected function populateStub(string $stub, string $name): string 80 + { 81 + return str_replace( 82 + ['{{ namespace }}', '{{ class }}'], 83 + [$this->getNamespace(), $name], 84 + $stub 85 + ); 86 + } 87 + 88 + protected function outputRegistrationHint(string $name, bool $isPublic): void 89 + { 90 + $this->newLine(); 91 + $this->components->info('Register the extension in your AppServiceProvider:'); 92 + $this->newLine(); 93 + 94 + $namespace = $this->getNamespace(); 95 + $extensionName = Str::of($name)->before('Client')->camel()->toString(); 96 + $clientClass = $isPublic ? 'AtpPublicClient' : 'AtpClient'; 97 + 98 + $this->line("use {$namespace}\\{$name};"); 99 + $this->line("use SocialDept\\AtpClient\\".($isPublic ? 'Client\\Public\\' : '').$clientClass.';'); 100 + $this->newLine(); 101 + $this->line("// In boot() method:"); 102 + $this->line("{$clientClass}::extend('{$extensionName}', fn(\${$clientClass} \$atp) => new {$name}(\$atp));"); 103 + } 104 + 105 + protected function getStub(): string 106 + { 107 + return <<<'STUB' 108 + <?php 109 + 110 + namespace {{ namespace }}; 111 + 112 + use SocialDept\AtpClient\AtpClient; 113 + 114 + class {{ class }} 115 + { 116 + protected AtpClient $atp; 117 + 118 + public function __construct(AtpClient $parent) 119 + { 120 + $this->atp = $parent; 121 + } 122 + 123 + // 124 + } 125 + STUB; 126 + } 127 + 128 + protected function getPublicStub(): string 129 + { 130 + return <<<'STUB' 131 + <?php 132 + 133 + namespace {{ namespace }}; 134 + 135 + use SocialDept\AtpClient\Client\Public\AtpPublicClient; 136 + 137 + class {{ class }} 138 + { 139 + protected AtpPublicClient $atp; 140 + 141 + public function __construct(AtpPublicClient $parent) 142 + { 143 + $this->atp = $parent; 144 + } 145 + 146 + // 147 + } 148 + STUB; 149 + } 150 + }
+146
src/Console/MakeAtpRequestCommand.php
··· 1 + <?php 2 + 3 + namespace SocialDept\AtpClient\Console; 4 + 5 + use Illuminate\Console\Command; 6 + use Illuminate\Filesystem\Filesystem; 7 + use Illuminate\Support\Str; 8 + 9 + class MakeAtpRequestCommand extends Command 10 + { 11 + protected $signature = 'make:atp-request 12 + {name : The name of the request client class} 13 + {--domain=bsky : The domain to extend (bsky, atproto, chat, ozone)} 14 + {--public : Generate a public request client instead of authenticated} 15 + {--force : Overwrite existing file}'; 16 + 17 + protected $description = 'Create a new ATP request client extension for an existing domain'; 18 + 19 + protected array $validDomains = ['bsky', 'atproto', 'chat', 'ozone']; 20 + 21 + public function __construct(protected Filesystem $files) 22 + { 23 + parent::__construct(); 24 + } 25 + 26 + public function handle(): int 27 + { 28 + $name = $this->argument('name'); 29 + $domain = $this->option('domain'); 30 + $isPublic = $this->option('public'); 31 + 32 + if (! in_array($domain, $this->validDomains)) { 33 + $this->components->error("Invalid domain [{$domain}]. Valid domains: ".implode(', ', $this->validDomains)); 34 + 35 + return self::FAILURE; 36 + } 37 + 38 + if (! Str::endsWith($name, 'Client')) { 39 + $name .= 'Client'; 40 + } 41 + 42 + $path = $this->getPath($name); 43 + 44 + if ($this->files->exists($path) && ! $this->option('force')) { 45 + $this->components->error("Request client [{$name}] already exists!"); 46 + 47 + return self::FAILURE; 48 + } 49 + 50 + $this->makeDirectory($path); 51 + 52 + $stub = $isPublic ? $this->getPublicStub() : $this->getStub(); 53 + $content = $this->populateStub($stub, $name); 54 + 55 + $this->files->put($path, $content); 56 + 57 + $this->components->info("Request client [{$path}] created successfully."); 58 + 59 + $this->outputRegistrationHint($name, $domain, $isPublic); 60 + 61 + return self::SUCCESS; 62 + } 63 + 64 + protected function getPath(string $name): string 65 + { 66 + $basePath = config('client.generators.request_path', 'app/Services/Clients/Requests'); 67 + 68 + return base_path($basePath.'/'.$name.'.php'); 69 + } 70 + 71 + protected function makeDirectory(string $path): void 72 + { 73 + if (! $this->files->isDirectory(dirname($path))) { 74 + $this->files->makeDirectory(dirname($path), 0755, true); 75 + } 76 + } 77 + 78 + protected function getNamespace(): string 79 + { 80 + $basePath = config('client.generators.request_path', 'app/Services/Clients/Requests'); 81 + 82 + return Str::of($basePath) 83 + ->replace('/', '\\') 84 + ->ucfirst() 85 + ->replace('App', 'App') 86 + ->toString(); 87 + } 88 + 89 + protected function populateStub(string $stub, string $name): string 90 + { 91 + return str_replace( 92 + ['{{ namespace }}', '{{ class }}'], 93 + [$this->getNamespace(), $name], 94 + $stub 95 + ); 96 + } 97 + 98 + protected function outputRegistrationHint(string $name, string $domain, bool $isPublic): void 99 + { 100 + $this->newLine(); 101 + $this->components->info('Register the extension in your AppServiceProvider:'); 102 + $this->newLine(); 103 + 104 + $namespace = $this->getNamespace(); 105 + $extensionName = Str::of($name)->before('Client')->camel()->toString(); 106 + $clientClass = $isPublic ? 'AtpPublicClient' : 'AtpClient'; 107 + 108 + $this->line("use {$namespace}\\{$name};"); 109 + $this->line("use SocialDept\\AtpClient\\".($isPublic ? 'Client\\Public\\' : '').$clientClass.';'); 110 + $this->newLine(); 111 + $this->line("// In boot() method:"); 112 + $this->line("{$clientClass}::extendDomain('{$domain}', '{$extensionName}', fn(\$domain) => new {$name}(\$domain));"); 113 + } 114 + 115 + protected function getStub(): string 116 + { 117 + return <<<'STUB' 118 + <?php 119 + 120 + namespace {{ namespace }}; 121 + 122 + use SocialDept\AtpClient\Client\Requests\Request; 123 + 124 + class {{ class }} extends Request 125 + { 126 + // 127 + } 128 + STUB; 129 + } 130 + 131 + protected function getPublicStub(): string 132 + { 133 + return <<<'STUB' 134 + <?php 135 + 136 + namespace {{ namespace }}; 137 + 138 + use SocialDept\AtpClient\Client\Public\Requests\PublicRequest; 139 + 140 + class {{ class }} extends PublicRequest 141 + { 142 + // 143 + } 144 + STUB; 145 + } 146 + }