-
-
Save mbabker/665da3e15ecc1479ba07 to your computer and use it in GitHub Desktop.
Factory Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sample Application | |
* | |
* @copyright Copyright (C) WebSpark, Inc. All rights reserved. | |
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later | |
*/ | |
namespace Sample; | |
use Joomla\DI\Container; | |
/** | |
* Application Factory | |
* | |
* @since 1.0 | |
*/ | |
class Factory | |
{ | |
/** | |
* DI Container | |
* | |
* @var Container | |
* @since 1.0 | |
*/ | |
private $container; | |
/** | |
* Singleton Factory instance | |
* | |
* @var Factory | |
* @since 1.0 | |
*/ | |
private static $instance; | |
/** | |
* Constructor | |
* | |
* @param Container $container DI Container | |
* | |
* @since 1.0 | |
*/ | |
public function __construct(Container $container) | |
{ | |
$this->container = $container; | |
self::$instance = $this; | |
} | |
/** | |
* Fetch the DI container | |
* | |
* @return Container | |
* | |
* @since 1.0 | |
*/ | |
public function getContainer() | |
{ | |
return $this->container; | |
} | |
/** | |
* Retrieves a helper class | |
* | |
* @param string $helper The helper object to retrieve | |
* | |
* @return object | |
* | |
* @since 1.0 | |
* @throws \InvalidArgumentException | |
*/ | |
private static function getHelper($helper) | |
{ | |
$class = 'Sample\\Helper\\' . ucfirst(strtolower($helper)) . 'Helper'; | |
// Make sure the helper exists | |
if (!class_exists($class)) | |
{ | |
/** @var \Joomla\Language\Text $text */ | |
$text = self::$instance->getContainer()->get('Joomla\\Language\\Text'); | |
throw new \InvalidArgumentException(sprintf('Helper class for "%s" not found.', $helper)); | |
} | |
// If the helper already exists in the DI Container, fetch the existing object | |
if (self::$instance->getContainer()->exists($class)) | |
{ | |
return self::$instance->getContainer()->get($class); | |
} | |
return self::$instance->getContainer()->buildObject($class); | |
} | |
/** | |
* Retrieves a model class | |
* | |
* @param string $model The model object to retrieve | |
* | |
* @return \Sample\Model\AbstractModel | |
* | |
* @since 1.0 | |
*/ | |
private static function getModel($model) | |
{ | |
$class = 'Sample\\Model\\' . ucfirst(strtolower($model)) . 'Model'; | |
// If the requested model does not exist, revert to the application default | |
if (!class_exists($class)) | |
{ | |
$class = 'Sample\\Model\\DefaultModel'; | |
} | |
// If the model already exists in the DI Container, fetch the existing object | |
if (self::$instance->getContainer()->exists($class)) | |
{ | |
return self::$instance->getContainer()->get($class); | |
} | |
return self::$instance->getContainer()->buildObject($class); | |
} | |
/** | |
* Retrieves the Repository object for the given entity | |
* | |
* @param string $entity The fully qualified class name of the entity | |
* | |
* @return \Sample\Entity\BaseRepository | |
* | |
* @since 1.0 | |
*/ | |
private static function getRepository($entity) | |
{ | |
$entity = 'Sample:' . $entity; | |
/** @var \Sample\Doctrine\ManagerRegistry $manager */ | |
$manager = self::get('doctrine'); | |
return $manager->getManager()->getRepository($entity); | |
} | |
/** | |
* Retrieves the active user | |
* | |
* @param integer $id User ID to retrieve | |
* | |
* @return \Sample\Entity\User | |
* | |
* @since 1.0 | |
*/ | |
private static function getUser($id = 0) | |
{ | |
return self::$instance->getContainer()->get('app')->getUser($id); | |
} | |
/** | |
* Retrieve objects from the DI container | |
* | |
* @param string $key Container lookup key | |
* | |
* @return mixed | |
* | |
* @note Method accepts additional params for special cases | |
* @since 1.0 | |
*/ | |
public static function get($key) | |
{ | |
$args = func_get_args(); | |
switch ($key) | |
{ | |
case 'helper' : | |
return self::getHelper($args[1]); | |
case 'model' : | |
return self::getModel($args[1]); | |
case 'repository' : | |
return self::getRepository($args[1]); | |
case 'user': | |
$id = isset($args[1]) ? $args[1] : 0; | |
return self::getUser($id); | |
default : | |
return self::$instance->getContainer()->get($key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment