PHP code example of icebox-php / framework

1. Go to this page and download the library: Download icebox-php/framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

icebox-php / framework example snippets


use Icebox\ActiveRecord\Config as ArConfig;

$database_url = (string) env('DATABASE_URL');

if($database_url == null) {
    throw new \LengthException('DATABASE_URL must have a value. example: mysql://username:password@host:port/database_name');
}

// parse the URL
$parsed = ArConfig::parseDatabaseUrl($database_url);

// Extract components
$dsn = $parsed['dsn'];
$username = $parsed['username'];
$password = $parsed['password'];
$options = $parsed['options'];

// User can customize options if needed
$options[PDO::ATTR_PERSISTENT] = true;

// Create PDO connection
$connection = new PDO($dsn, $username, $password, $options);

// Initialize ActiveRecord
ArConfig::initialize($connection);

use Icebox\Model;

class User extends Model
{
    // Optional: specify custom table name
    protected static $table = 'users';
    
    // Optional: specify custom primary key
    protected static $primaryKey = 'id';
}

class Post extends Model
{
    // Table name will be inferred as 'posts'
    // Primary key will default to 'id'
}

// Create
$user = new User(['name' => 'John', 'email' => '[email protected]']);
$user->save();

// Read by primary key
$user = User::find(1);

// Update
$user = User::find(1);
$user->name = 'Jane';
$user->save();

// Or update multiple attributes
$user->updateAttributes(['name' => 'Jane', 'email' => '[email protected]']);

// Delete
$user->delete();

// Equality (multiple syntaxes)
User::where('name', 'John')->get();
User::where(['name' => 'John', 'active' => 1])->get();

// Comparison operators
User::where('age', '>', 18)->get();
User::where('age', '>=', 21)->get();
User::where('name', 'LIKE', 'John%')->get();

// NULL checks (Rails-style)
User::where('approved_at', null)->get();           // IS NULL
User::where('approved_at', '!=', null)->get();     // IS NOT NULL
User::whereNull('deleted_at')->get();
User::whereNotNull('approved_at')->get();

// IN clauses
User::where('id', [1, 2, 3])->get();               // IN (1,2,3)
User::whereIn('category_id', [1, 2, 3])->get();
User::whereNotIn('status', ['banned', 'inactive'])->get();

// BETWEEN
User::whereBetween('age', [18, 65])->get();

// OR conditions
User::where(['or' => ['status' => 'active', 'status' => 'pending']])->get();
User::where('status', 'active')->orWhere('status', 'pending')->get();

// Raw SQL
User::where('price > cost * ?', [1.5])->get();

// Get all records
User::where()->get();
User::where([])->get();

User::where('active', 1)
    ->where('age', '>', 18)
    ->orderBy('name', 'DESC')
    ->limit(10)
    ->offset(5)
    ->get();

// Count records
$count = User::where('active', 1)->count();

// Check if exists
$exists = User::where('email', '[email protected]')->exists();

// Get first result
$user = User::where('active', 1)->first();

\Icebox\ActiveRecord\Connection::beginTransaction();
try {
    $user1->save();
    $user2->save();
    \Icebox\ActiveRecord\Connection::commit();
} catch (Exception $e) {
    \Icebox\ActiveRecord\Connection::rollback();
}

use Icebox\Log;

// Add handlers (no default handlers - you must add at least one)
Log::addFileHandler('log/development.log');
Log::addStdoutHandler(); // For CLI output

// Log messages
Log::info('User logged in');
Log::error('Database connection failed', ['db' => 'primary']);
Log::debug('Processing request', ['method' => 'GET']);

// All PSR-3 levels are supported:
// emergency, alert, critical, error, warning, notice, info, debug

Log::addFileHandler(
    'log/development.log',  // path
    'debug',                 // level (optional, default: debug)
    7                        // max files to keep (optional, default: 7)
);

Log::addSyslogHandler(
    'myapp',      // ident (optional, default: icebox)
    LOG_USER,     // facility (optional, default: LOG_USER)
    'warning'     // level (optional, default: debug)
);

Log::addStdoutHandler(
    'info'  // level (optional, default: debug)
);
// Colorful output in CLI, plain output in web

Log::addClosureHandler(function($log) {
    // $log object contains:
    // - level: 'debug', 'info', 'warning', 'error', etc.
    // - message: The log message
    // - context: Additional context data
    // - channel: Logger channel name
    // - datetime: When the log occurred
    
    // Send to monitoring services (Sentry, Airbrake, etc.)
    if ($log->level === 'error') {
        \Sentry\captureMessage($log->message, $log->context);
    }
    
    // Or send to Slack, email, etc.
}, 'error');  // level (optional, default: debug)

// Development setup
Log::addFileHandler('log/development.log');
Log::addStdoutHandler();

// Production setup
Log::addFileHandler('log/production.log', 'info');
Log::addSyslogHandler('myapp-prod', 'error', LOG_USER);
Log::addClosureHandler(function($log) {
    // Send critical errors to monitoring
    if (in_array($log->level, ['error', 'critical', 'alert', 'emergency'])) {
        sendToMonitoringService($log);
    }
}, 'error');

use Icebox\Log;

// Clear handlers for testing
Log::clearHandlers();

// Add test handlers
Log::addClosureHandler(function($log) {
    echo "Test log: {$log->level} - {$log->message}\n";
});

// Check handler count
echo Log::handlerCount(); // 1
bash
php icebox console
php icebox c  # Short alias
bash
$ php icebox console
Loading development environment (PHP 8.5.0)
Type 'exit' to quit, 'help' for help.

Psy Shell v0.12.17 (PHP 8.5.0 — cli) by Justin Hileman
>>> echo App::root_url();
http://localhost/myapp
>>> $user = User::find(1);
>>> echo $user->name;
John Doe
>>> exit
bash
php icebox test
bash
php icebox test tests/create_user_test.php
php icebox test tests/Feature/
bash
php icebox test --filter testCreateUser
php icebox test tests/UserTest.php --filter testCreateUser # more specific
php icebox test --filter testCreateUser
php icebox test --coverage-html coverage

'boolean' => array( 'html_tag' => 'checkbox', 'type' => ''),
'date' => array( 'html_tag' => 'input', 'type' => 'date'),
'datetime' => array( 'html_tag' => 'input', 'type' => 'datetime-local'),
'decimal' => array( 'html_tag' => 'input', 'type' => 'number'),
<br>
'float' => array( 'html_tag' => 'input', 'type' => 'number'),
'integer' => array( 'html_tag' => 'input', 'type' => 'number'),
'string' => array( 'html_tag' => 'input', 'type' => 'text'),
'text' => array( 'html_tag' => 'textarea', 'type' => ''),
<br>
'time' => array( 'html_tag' => 'input', 'type' => 'time'),