|
| 1 | +/* Laravel wizard using posthog-agent with PostHog MCP */ |
| 2 | +import type { WizardOptions } from '../utils/types'; |
| 3 | +import type { FrameworkConfig } from '../lib/framework-config'; |
| 4 | +import { enableDebugLogs } from '../utils/debug'; |
| 5 | +import { runAgentWizard } from '../lib/agent-runner'; |
| 6 | +import { Integration } from '../lib/constants'; |
| 7 | +import clack from '../utils/clack'; |
| 8 | +import chalk from 'chalk'; |
| 9 | +import * as semver from 'semver'; |
| 10 | +import { |
| 11 | + getLaravelVersion, |
| 12 | + getLaravelProjectType, |
| 13 | + getLaravelProjectTypeName, |
| 14 | + getLaravelVersionBucket, |
| 15 | + LaravelProjectType, |
| 16 | + findLaravelServiceProvider, |
| 17 | + findLaravelBootstrapFile, |
| 18 | + detectLaravelStructure, |
| 19 | +} from './utils'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Laravel framework configuration for the universal agent runner |
| 23 | + */ |
| 24 | +const MINIMUM_LARAVEL_VERSION = '9.0.0'; |
| 25 | + |
| 26 | +const LARAVEL_AGENT_CONFIG: FrameworkConfig = { |
| 27 | + metadata: { |
| 28 | + name: 'Laravel', |
| 29 | + integration: Integration.laravel, |
| 30 | + docsUrl: 'https://posthog.com/docs/libraries/php', |
| 31 | + unsupportedVersionDocsUrl: 'https://posthog.com/docs/libraries/php', |
| 32 | + gatherContext: async (options: WizardOptions) => { |
| 33 | + const projectType = await getLaravelProjectType(options); |
| 34 | + const serviceProvider = await findLaravelServiceProvider(options); |
| 35 | + const bootstrapFile = findLaravelBootstrapFile(options); |
| 36 | + const laravelStructure = detectLaravelStructure(options); |
| 37 | + |
| 38 | + return { |
| 39 | + projectType, |
| 40 | + serviceProvider, |
| 41 | + bootstrapFile, |
| 42 | + laravelStructure, |
| 43 | + }; |
| 44 | + }, |
| 45 | + }, |
| 46 | + |
| 47 | + detection: { |
| 48 | + packageName: 'laravel/framework', |
| 49 | + packageDisplayName: 'Laravel', |
| 50 | + usesPackageJson: false, |
| 51 | + getVersion: (_packageJson: any) => { |
| 52 | + // For Laravel, we don't use package.json. Version is extracted separately |
| 53 | + // from composer.json in the wizard entry point |
| 54 | + return undefined; |
| 55 | + }, |
| 56 | + getVersionBucket: getLaravelVersionBucket, |
| 57 | + }, |
| 58 | + |
| 59 | + environment: { |
| 60 | + uploadToHosting: false, |
| 61 | + getEnvVars: (apiKey: string, host: string) => ({ |
| 62 | + POSTHOG_API_KEY: apiKey, |
| 63 | + POSTHOG_HOST: host, |
| 64 | + }), |
| 65 | + }, |
| 66 | + |
| 67 | + analytics: { |
| 68 | + getTags: (context: any) => { |
| 69 | + const projectType = context.projectType as LaravelProjectType; |
| 70 | + return { |
| 71 | + projectType: projectType || 'unknown', |
| 72 | + laravelStructure: context.laravelStructure || 'unknown', |
| 73 | + }; |
| 74 | + }, |
| 75 | + }, |
| 76 | + |
| 77 | + prompts: { |
| 78 | + projectTypeDetection: |
| 79 | + 'This is a PHP/Laravel project. Look for composer.json, artisan CLI, and app/ directory structure to confirm. Check for Laravel-specific packages like laravel/framework.', |
| 80 | + packageInstallation: |
| 81 | + 'Use Composer to install packages. Run `composer require posthog/posthog-php` without pinning a specific version.', |
| 82 | + getAdditionalContextLines: (context: any) => { |
| 83 | + const projectType = context.projectType as LaravelProjectType; |
| 84 | + const projectTypeName = projectType |
| 85 | + ? getLaravelProjectTypeName(projectType) |
| 86 | + : 'unknown'; |
| 87 | + |
| 88 | + const lines = [ |
| 89 | + `Project type: ${projectTypeName}`, |
| 90 | + `Framework docs ID: php (use posthog://docs/frameworks/php for documentation)`, |
| 91 | + `Laravel structure: ${context.laravelStructure} (affects where to add configuration)`, |
| 92 | + ]; |
| 93 | + |
| 94 | + if (context.serviceProvider) { |
| 95 | + lines.push(`Service provider: ${context.serviceProvider}`); |
| 96 | + } |
| 97 | + |
| 98 | + if (context.bootstrapFile) { |
| 99 | + lines.push(`Bootstrap file: ${context.bootstrapFile}`); |
| 100 | + } |
| 101 | + |
| 102 | + // Add Laravel-specific guidance based on version structure |
| 103 | + if (context.laravelStructure === 'latest') { |
| 104 | + lines.push( |
| 105 | + 'Note: Laravel 11+ uses simplified bootstrap/app.php for middleware and providers', |
| 106 | + ); |
| 107 | + } else { |
| 108 | + lines.push( |
| 109 | + 'Note: Use app/Http/Kernel.php for middleware, app/Providers for service providers', |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + return lines; |
| 114 | + }, |
| 115 | + }, |
| 116 | + |
| 117 | + ui: { |
| 118 | + successMessage: 'PostHog integration complete', |
| 119 | + estimatedDurationMinutes: 5, |
| 120 | + getOutroChanges: (context: any) => { |
| 121 | + const projectType = context.projectType as LaravelProjectType; |
| 122 | + const projectTypeName = projectType |
| 123 | + ? getLaravelProjectTypeName(projectType) |
| 124 | + : 'Laravel'; |
| 125 | + |
| 126 | + const changes = [ |
| 127 | + `Analyzed your ${projectTypeName} project structure`, |
| 128 | + `Installed the PostHog PHP package via Composer`, |
| 129 | + `Configured PostHog in your Laravel application`, |
| 130 | + ]; |
| 131 | + |
| 132 | + if (context.laravelStructure === 'latest') { |
| 133 | + changes.push('Added PostHog initialization to bootstrap/app.php'); |
| 134 | + } else { |
| 135 | + changes.push('Created a PostHog service provider for initialization'); |
| 136 | + } |
| 137 | + |
| 138 | + if (projectType === LaravelProjectType.INERTIA) { |
| 139 | + changes.push('Configured PostHog to work with Inertia.js'); |
| 140 | + } |
| 141 | + |
| 142 | + if (projectType === LaravelProjectType.LIVEWIRE) { |
| 143 | + changes.push('Configured PostHog to work with Livewire'); |
| 144 | + } |
| 145 | + |
| 146 | + return changes; |
| 147 | + }, |
| 148 | + getOutroNextSteps: () => [ |
| 149 | + 'Start your Laravel development server with `php artisan serve`', |
| 150 | + 'Visit your PostHog dashboard to see incoming events', |
| 151 | + 'Use PostHog::capture() to track custom events', |
| 152 | + 'Use PostHog::identify() to associate events with users', |
| 153 | + ], |
| 154 | + }, |
| 155 | +}; |
| 156 | + |
| 157 | +/** |
| 158 | + * Laravel wizard powered by the universal agent runner. |
| 159 | + */ |
| 160 | +export async function runLaravelWizardAgent( |
| 161 | + options: WizardOptions, |
| 162 | +): Promise<void> { |
| 163 | + if (options.debug) { |
| 164 | + enableDebugLogs(); |
| 165 | + } |
| 166 | + |
| 167 | + // Check Laravel version - agent wizard requires >= 9.0.0 |
| 168 | + const laravelVersion = getLaravelVersion(options); |
| 169 | + |
| 170 | + if (laravelVersion) { |
| 171 | + const coercedVersion = semver.coerce(laravelVersion); |
| 172 | + if (coercedVersion && semver.lt(coercedVersion, MINIMUM_LARAVEL_VERSION)) { |
| 173 | + const docsUrl = |
| 174 | + LARAVEL_AGENT_CONFIG.metadata.unsupportedVersionDocsUrl ?? |
| 175 | + LARAVEL_AGENT_CONFIG.metadata.docsUrl; |
| 176 | + |
| 177 | + clack.log.warn( |
| 178 | + `Sorry: the wizard can't help you with Laravel ${laravelVersion}. Upgrade to Laravel ${MINIMUM_LARAVEL_VERSION} or later, or check out the manual setup guide.`, |
| 179 | + ); |
| 180 | + clack.log.info(`Setup Laravel manually: ${chalk.cyan(docsUrl)}`); |
| 181 | + clack.outro('PostHog wizard will see you next time!'); |
| 182 | + return; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + await runAgentWizard(LARAVEL_AGENT_CONFIG, options); |
| 187 | +} |
0 commit comments