コード例 #1
0
ファイル: db_seed.php プロジェクト: endel/hook-cli
            } catch (Exception $e) {
                echo "Failed.";
            }
            echo PHP_EOL;
        }
        if (isset($options['data']) && $options['data']) {
            $current_row = 0;
            $total_rows = count($options['data']);
            foreach ($options['data'] as $data) {
                // Look for special data fields
                foreach ($data as $field => $value) {
                    if (preg_match('/\\!upload ([^$]+)/', $value, $file)) {
                        $filepath = Project::root(Project::DIRECTORY_NAME) . '/seeds/' . $file[1];
                        // stop when file doens't exists
                        if (!file_exists($filepath)) {
                            throw new Exception("File not found: '{$filepath}'");
                        }
                        $mime_type = Utils::mime_type($filepath);
                        $data[$field] = 'data:' . $mime_type . ';base64,' . base64_encode(file_get_contents($filepath));
                    }
                }
                $client->post('collection/' . $collection, array('data' => $data));
                $current_row += 1;
                $percent = round($current_row / $total_rows * 100);
                echo "Seeding '{$collection}': " . "{$percent}%" . str_repeat("\r", strlen($percent) + 1);
            }
        }
        echo PHP_EOL;
    }
    echo "Done." . PHP_EOL;
});