ICanBoogie/bind-activerecord v0.2.0
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Binding
      • ActiveRecord

Classes

  • Hooks
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\Binding\ActiveRecord;

use ICanBoogie\ActiveRecord\Connection;
use ICanBoogie\ActiveRecord\ConnectionCollection;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\ModelCollection;
use ICanBoogie\ActiveRecord\RunTimeActiveRecordCache;
use ICanBoogie\Core;

class Hooks
{
    /**
     * Synthesizes a config from a namespace.
     *
     * The config fragments found in the namespace are merged with `array_merge()`.
     *
     * @param array $fragments
     * @param string $namespace
     *
     * @return array
     */
    static private function synthesize_config_from_namespace(array $fragments, $namespace)
    {
        $config = [];

        foreach ($fragments as $fragment)
        {
            if (empty($fragment[$namespace]))
            {
                continue;
            }

            $config[] = $fragment[$namespace];
        }

        return $config ? array_merge(...$config) : [];
    }

    /**
     * Synthesizes the `activerecord_connections` config from `activerecord#connections` fragments.
     *
     * @param array $fragments
     *
     * @return array
     */
    static public function synthesize_connections_config(array $fragments)
    {
        return self::synthesize_config_from_namespace($fragments, 'connections');
    }

    /**
     * Synthesizes the `activerecord_models` config from `activerecord#models` fragments.
     *
     * @param array $fragments
     *
     * @return array
     */
    static public function synthesize_models_config(array $fragments)
    {
        return self::synthesize_config_from_namespace($fragments, 'models');
    }

    /*
     * Prototypes
     */

    /**
     * Returns a @{link ConnectionCollection} instance configured with
     * the `activerecord_connections` config.
     *
     * @param Core $app
     *
     * @return ConnectionCollection
     */
    static public function core_lazy_get_connections(Core $app)
    {
        static $connections;

        if (!$connections)
        {
            $connections = new ConnectionCollection($app->configs['activerecord_connections'] ?: []);
        }

        return $connections;
    }

    /**
     * Returns a @{link ModelCollection} instance configured with
     * the `activerecord_models` config.
     *
     * @param Core $app
     *
     * @return ModelCollection
     */
    static public function core_lazy_get_models(Core $app)
    {
        static $models;

        if (!$models)
        {
            $models = new ModelCollection($app->connections, $app->configs['activerecord_models'] ?: []);
        }

        return $models;
    }

    /**
     * Getter for the "primary" database connection.
     *
     * @param Core $app
     *
     * @return Connection
     */
    static public function core_lazy_get_db(Core $app)
    {
        return $app->connections['primary'];
    }

    /**
     * Returns the records cache associated with the model.
     *
     * @param Model $model
     *
     * @return RunTimeActiveRecordCache
     */
    static public function model_lazy_get_activerecord_cache(Model $model)
    {
        return new RunTimeActiveRecordCache($model);
    }
}
ICanBoogie/bind-activerecord v0.2.0 API documentation generated by ApiGen