Files
scoutiq/cli.php

106 lines
3.5 KiB
PHP

<?php
if (php_sapi_name() !== 'cli') {
die("This script can only be run from the command line.");
}
require __DIR__ . '/vendor/autoload.php';
// Bootstrap Environment configurations
$localEnv = __DIR__ . '/.env';
$parentEnv = __DIR__ . '/../..';
if (file_exists($localEnv)) {
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
} elseif (file_exists($parentEnv . '/.env')) {
$dotenv = Dotenv\Dotenv::createImmutable($parentEnv);
$dotenv->load();
}
require_once __DIR__ . '/database/seeds/DatabaseSeeder.php';
use App\Core\Container;
use App\Services\Database\Connection;
use App\Services\Database\MigrationRunner;
use App\Services\Crawler\RssParser;
use App\Services\Crawler\AiAnalyzer;
use App\Services\Crawler\Collector;
use Database\Seeds\DatabaseSeeder;
$container = new Container();
// Singletons configurations
$container->singleton(Connection::class, Connection::class);
$container->singleton(MigrationRunner::class, MigrationRunner::class);
$container->singleton(DatabaseSeeder::class, DatabaseSeeder::class);
// Crawler service (needed for CLI collect command)
$container->singleton(RssParser::class, RssParser::class);
$container->singleton(AiAnalyzer::class, AiAnalyzer::class);
$container->singleton(Collector::class, Collector::class);
$args = $argv;
$command = $args[1] ?? 'help';
switch ($command) {
case 'migrate':
echo "=== Running Migrations ===\n";
try {
$runner = $container->get(MigrationRunner::class);
$runner->migrate();
} catch (\Throwable $e) {
echo "Migration failed: " . $e->getMessage() . "\n";
exit(1);
}
break;
case 'seed':
echo "=== Running Database Seeder ===\n";
try {
$seeder = $container->get(DatabaseSeeder::class);
$seeder->seed();
} catch (\Throwable $e) {
echo "Seeding failed: " . $e->getMessage() . "\n";
exit(1);
}
break;
case 'collect':
echo "=== Running Data Collector ===\n";
try {
$collector = $container->get(Collector::class);
$results = $collector->collectAll();
echo "\n--- Results ---\n";
echo "Sources processed: {$results['processed']}/{$results['total_sources']}\n";
echo "Errors: {$results['errors']}\n";
echo "New opportunities: {$results['new_opportunities']}\n";
echo "New organizations: {$results['new_organizations']}\n\n";
foreach ($results['details'] as $detail) {
echo "[{$detail['status']}] {$detail['source']} ({$detail['type']}): ";
if ($detail['status'] === 'success') {
echo "{$detail['entries_found']} entries, {$detail['new_opportunities']} opps, {$detail['new_organizations']} orgs\n";
} else {
echo "ERROR: {$detail['error']}\n";
}
}
} catch (\Throwable $e) {
echo "Collection failed: " . $e->getMessage() . "\n";
exit(1);
}
break;
case 'help':
default:
echo "ScoutIQ CLI Utility\n";
echo "Usage: php cli.php [command]\n\n";
echo "Commands:\n";
echo " migrate Run SQL database migrations\n";
echo " seed Seed the database with default data\n";
echo " collect Run data collector (RSS feeds)\n";
echo " help Show this help menu\n";
break;
}