3 namespace RedBeanPHP\BeanHelper;
5 use RedBeanPHP\BeanHelper as BeanHelper;
6 use RedBeanPHP\Facade as Facade;
7 use RedBeanPHP\OODBBean as OODBBean;
8 use RedBeanPHP\SimpleModelHelper as SimpleModelHelper;
13 * The Bean helper helps beans to access access the toolbox and
14 * FUSE models. This Bean Helper makes use of the facade to obtain a
15 * reference to the toolbox.
17 * @file RedBeanPHP/BeanHelperFacade.php
18 * @author Gabor de Mooij and the RedBeanPHP Community
22 * (c) copyright G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
23 * This source file is subject to the BSD/GPLv2 License that is bundled
24 * with this source code in the file license.txt.
26 class SimpleFacadeBeanHelper implements BeanHelper
29 * Factory function to create instance of Simple Model, if any.
33 private static $factory = null;
36 * Factory method using a customizable factory function to create
37 * the instance of the Simple Model.
39 * @param string $modelClassName name of the class
43 public static function factory( $modelClassName )
45 $factory = self::$factory;
46 return ( $factory ) ? $factory( $modelClassName ) : new $modelClassName();
50 * Sets the factory function to create the model when using FUSE
51 * to connect a bean to a model.
53 * @param \Closure $factory factory function
57 public static function setFactoryFunction( $factory )
59 self::$factory = $factory;
63 * @see BeanHelper::getToolbox
65 public function getToolbox()
67 return Facade::getToolBox();
71 * @see BeanHelper::getModelForBean
73 public function getModelForBean( OODBBean $bean )
75 $model = $bean->getMeta( 'type' );
76 $prefix = defined( 'REDBEAN_MODEL_PREFIX' ) ? REDBEAN_MODEL_PREFIX : '\\Model_';
78 if ( strpos( $model, '_' ) !== FALSE ) {
79 $modelParts = explode( '_', $model );
81 foreach( $modelParts as $part ) {
82 $modelName .= ucfirst( $part );
84 $modelName = $prefix . $modelName;
86 if ( !class_exists( $modelName ) ) {
88 $modelName = $prefix . ucfirst( $model );
90 if ( !class_exists( $modelName ) ) {
97 $modelName = $prefix . ucfirst( $model );
98 if ( !class_exists( $modelName ) ) {
102 $obj = self::factory( $modelName );
103 $obj->loadBean( $bean );
109 * @see BeanHelper::getExtractedToolbox
111 public function getExtractedToolbox()
113 return Facade::getExtractedToolbox();