src/Fabricate.php
public static function define($name, $define)
    {
        $instance = self::getInstance();
        $parent = false;
        $base   = false;
        $trait  = false;
        if (is_array($name)) {
            $parent = array_key_exists('parent', $name)?$name['parent']:false;
            $base   = array_key_exists('class', $name)?$name['class']:false;
            if (array_key_exists('trait', $name)) {
                $name = $name['trait'];
                $parent = $base = false;
                $trait = true;
            } else {
                $name = $name[0];
            }
        }
        if (empty($name)) {
            throw new \InvalidArgumentException("name is empty");
        }
        if ($parent && !$instance->registry->is_registered($parent)) {
            throw new \InvalidArgumentException("parent `{$parent}` is not registered");
        }
        if ($base && in_array($instance->config->adaptor->getModel($base), [false, null])) {
            throw new \InvalidArgumentException("class `{$base}` is not found");
        }
        $definition = new FabricateDefinition($define);
        if ($trait) {
            $instance->traits[$name] = $definition;
            return;
        }
        if (!$parent && !$base) {
            $base = $name;
        }
        $definition->parent = $parent?FabricateFactory::create($instance->registry->find($parent)):false;
        $definition->parent = $base?FabricateFactory::create($instance->config->adaptor->getModel($base)):$definition->parent;
        $definition->parent->setConfig(self::getInstance()->config);

        $instance->registry->register($name, $definition);
    }
public static function define($name, $define)
    {
        $instance = self::getInstance();
        $parent = false;
        $base   = false;
        $trait  = false;
        if (is_array($name)) {
            $parent = array_key_exists('parent', $name)?$name['parent']:false;
            $base   = array_key_exists('class', $name)?$name['class']:false;
            if (array_key_exists('trait', $name)) {
                $name = $name['trait'];
                $parent = $base = false;
                $trait = true;
            } else {
                $name = $name[0];
            }
        }
        if (empty($name)) {
            throw new \InvalidArgumentException("name is empty");
        }
        if ($parent && !$instance->registry->is_registered($parent)) {
            throw new \InvalidArgumentException("parent `{$parent}` is not registered");
        }
        if ($base && in_array($instance->config->adaptor->getModel($base), [false, null])) {
            throw new \InvalidArgumentException("class `{$base}` is not found");
        }
        $definition = new FabricateDefinition($define);
        if ($trait) {
            $instance->traits[$name] = $definition;
            return;
        }
        if (!$parent && !$base) {
            $base = $name;
        }
        $definition->parent = $parent?FabricateFactory::create($instance->registry->find($parent)):false;
        $definition->parent = $base?FabricateFactory::create($instance->config->adaptor->getModel($base)):$definition->parent;
        $definition->parent->setConfig(self::getInstance()->config);

        $instance->registry->register($name, $definition);
    }
public static function define ( $ name , $ define ) { $ instance = self :: getInstance ( ) ; $ parent = false ; $ base = false ; $ trait = false ; if ( is_array ( $ name ) ) { $ parent = array_key_exists ( 'parent' , $ name ) ? $ name [ 'parent' ] : false ; $ base = array_key_exists ( 'class' , $ name ) ? $ name [ 'class' ] : false ; if ( array_key_exists ( 'trait' , $ name ) ) { $ name = $ name [ 'trait' ] ; $ parent = $ base = false ; $ trait = true ; } else { $ name = $ name [ 0 ] ; } } if ( empty ( $ name ) ) { throw new \ InvalidArgumentException ( "name is empty" ) ; } if ( $ parent && ! $ instance -> registry -> is_registered ( $ parent ) ) { throw new \ InvalidArgumentException ( "parent `{$parent}` is not registered" ) ; } if ( $ base && in_array ( $ instance -> config -> adaptor -> getModel ( $ base ) , [ false , null ] ) ) { throw new \ InvalidArgumentException ( "class `{$base}` is not found" ) ; } $ definition = new FabricateDefinition ( $ define ) ; if ( $ trait ) { $ instance -> traits [ $ name ] = $ definition ; return ; } if ( ! $ parent && ! $ base ) { $ base = $ name ; } $ definition -> parent = $ parent ? FabricateFactory :: create ( $ instance -> registry -> find ( $ parent ) ) : false ; $ definition -> parent = $ base ? FabricateFactory :: create ( $ instance -> config -> adaptor -> getModel ( $ base ) ) : $ definition -> parent ; $ definition -> parent -> setConfig ( self :: getInstance ( ) -> config ) ; $ instance -> registry -> register ( $ name , $ definition ) ; }
Define fabrication object

@param mixed $name name or with attributes
@param mixed $define definition
@return void
@throws InvalidArgumentException
Define fabrication object