/** * Define state machine using $machine_definition. */ public function initializeMachine($args) { parent::initializeMachine($args); }
/** * Define state machine used by all instances of this type. */ protected function initializeMachine($config) { // Load simple config options $this->initializeMachineConfig($config, ['table', 'table_alias', 'state_select']); // Get flupdo resource $this->flupdo = $this->getContext(isset($config['flupdo_resource']) ? $config['flupdo_resource'] : 'database'); if (!$this->flupdo instanceof \Smalldb\Flupdo\IFlupdo) { throw new InvalidArgumentException('Flupdo resource does not implement \\Smalldb\\Flupdo\\IFlupdo.'); } // Get authenticator $auth_resource_name = isset($config['auth_resource']) ? $config['auth_resource'] : 'auth'; $this->auth = $this->getContext($auth_resource_name); if (!$this->auth) { throw new InvalidArgumentException('Authenticator is missing.'); } if (!$this->auth instanceof \Smalldb\StateMachine\Auth\IAuth) { throw new InvalidArgumentException('Authenticator resource is not an instance of \\Smalldb\\StateMachine\\Auth\\IAuth.'); } // Get sphinx resource (optional) $sphinx_resource_name = isset($config['sphinx_resource']) ? $config['sphinx_resource'] : null; if ($sphinx_resource_name) { $this->sphinx = $this->getContext($sphinx_resource_name); if (!$this->sphinx instanceof \Smalldb\Flupdo\IFlupdo) { throw new InvalidArgumentException('Sphinx resource does not implement \\Smalldb\\Flupdo\\IFlupdo.'); } } else { $this->sphinx = null; } // Properties (unless set before) if ($this->properties === null) { if (empty($config['properties'])) { // Scan database for properties if not specified $this->scanTableColumns(); } } // Setup machine. Properties may be set before initializing, // this will merge config with autodetected properties parent::initializeMachine($config); // Collect primary key from properties if ($this->pk_columns === null) { $this->pk_columns = array(); foreach ($this->properties as $property => $p) { if (!empty($p['is_pk'])) { $this->pk_columns[] = $property; } } } // Check for primary key if (empty($this->pk_columns)) { throw new InvalidArgumentException('Primary key is missing in table ' . var_export($this->table, true)); } // Prepare list of composed properties and encoded columns $this->composed_properties = array(); $this->json_columns = array(); foreach ($this->properties as $property => $p) { if (!empty($p['components'])) { $this->composed_properties[$property] = $p['components']; } if (isset($p['column_encoding']) && $p['column_encoding'] == 'json') { $this->json_columns[] = $property; } } }