ICanBoogie/bind-activerecord master
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Binding
      • ActiveRecord

Classes

  • Hooks

Traits

  • CoreBindings
  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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
<?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;
use ICanBoogie\ActiveRecord\Connection;
use ICanBoogie\ActiveRecord\ConnectionCollection;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\ModelCollection;
use ICanBoogie\ActiveRecord\ActiveRecordCache\RuntimeActiveRecordCache;
use ICanBoogie\Core;
use ICanBoogie\Validate\ValidationErrors;

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 ? call_user_func_array('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');
    }

    /*
     * Events
     */

    /**
     * Patches the `get_model()` helper to use the model collection bound to the application.
     *
     * @param Core\BootEvent $event
     * @param Core|CoreBindings $app
     */
    static public function on_core_boot(Core\BootEvent $event, Core $app)
    {
        ActiveRecord\Helpers::patch('get_model', function($id) use ($app) {

            return $app->models[$id];

        });
    }

    /*
     * 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|CoreBindings $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|CoreBindings $app
     *
     * @return Connection
     */
    static public function core_lazy_get_db(Core $app)
    {
        return $app->connections['primary'];
    }

    /**
     * @param ActiveRecord $record
     *
     * @return ValidationErrors|array
     */
    static public function active_record_validate(ActiveRecord $record)
    {
        static $validate;

        if (!$validate)
        {
            $validate = new ActiveRecord\Validate\ValidateActiveRecord;
        }

        return $validate($record);
    }

    /**
     * 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 master API documentation generated by ApiGen