Facade in Symfony
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 | |
namespace AppBundle\Entity; | |
use AppBundle\DependencyInjection\Container; | |
class ActiveRecord extends Container | |
{ | |
protected static function getDoctrine() | |
{ | |
if (!self::$container->has('doctrine')) { | |
throw new \LogicException('The DoctrineBundle is not registered in your application.'); | |
} | |
return self::$container->get('doctrine'); | |
} | |
protected static function getEntityManager() | |
{ | |
return self::getDoctrine()->getManagerForClass(get_called_class()); | |
} | |
protected static function getRepository() | |
{ | |
return self::getDoctrine()->getRepository(get_called_class()); | |
} | |
/** | |
* @param $id | |
* @return null|static | |
*/ | |
public static function find($id) | |
{ | |
return self::getRepository()->find($id); | |
} | |
/** | |
* @return static[] | |
*/ | |
public static function findAll() | |
{ | |
return self::getRepository()->findAll(); | |
} | |
/** | |
* @return static[] | |
*/ | |
public static function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | |
{ | |
return self::getRepository()->findBy($criteria, $orderBy, $limit, $offset); | |
} | |
/** | |
* @return null|static | |
*/ | |
public static function findOneBy(array $criteria) | |
{ | |
return self::getRepository()->findOneBy($criteria); | |
} | |
} |
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 | |
namespace AppBundle; | |
use AppBundle\DependencyInjection\Container; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\HttpKernel\Bundle\Bundle; | |
class AppBundle extends Bundle | |
{ | |
public function setContainer(ContainerInterface $container = null) | |
{ | |
$this->container = $container; | |
Container::set($container); | |
} | |
} |
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 | |
namespace AppBundle\DependencyInjection; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
class Container | |
{ | |
/** | |
* @var ContainerInterface | |
*/ | |
static protected $container; | |
public static function set(ContainerInterface $container) | |
{ | |
self::$container = $container; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don’t know. Use laravel)))