Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added RedisExample Module

  • Loading branch information...
commit 620851a12d266dd89eff6e04036beaccfa383109 1 parent 6b876ed
@missinglink authored
View
30 Application/Module/RedisExample/Bootstrap.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Application\Module\RedisExample;
+
+use \Insomnia\Kernel,
+ \Insomnia\Pattern\KernelModule;
+
+/**
+ * Insomnia default welcome screen
+ *
+ * Provides a front page for the application.
+ *
+ * @insomnia:Module
+ */
+class Bootstrap extends KernelModule
+{
+ /**
+ * Module configuration
+ *
+ * @insomnia:KernelPlugins({
+ * @insomnia:Endpoint( class="Controller\RedisController" )
+ * })
+ *
+ * @param Kernel $kernel
+ */
+ public function run( Kernel $kernel )
+ {
+ $kernel->addEndpoint( __NAMESPACE__ . '\Controller\RedisController' );
+ }
+}
View
111 Application/Module/RedisExample/Bootstrap/Doctrine.php
@@ -0,0 +1,111 @@
+<?php
+
+namespace Application\Module\RedisExample\Bootstrap;
+
+use \Doctrine\Common\ClassLoader,
+ \Doctrine\ORM\Mapping\Driver\DriverChain,
+ \Doctrine\ORM\EntityManager,
+ \Doctrine\ORM\Configuration,
+ \Doctrine\Common\Cache\ArrayCache,
+ \Doctrine\Common\Cache\ApcCache;
+
+class Doctrine extends EntityManager
+{
+ public static $em;
+
+ const DATABASE = 'test';
+ const HOST = 'localhost';
+ const USER = 'php';
+ const PASS = '3889y23b4jh2bhjh5vjv2jh3vjhv5j23tg545';
+ const CHARSET = 'UTF8';
+ const DRIVER = 'pdo_mysql';
+
+ public function __construct()
+ {
+ // Autoloader
+ $classLoader = new ClassLoader( 'Entities', dirname( __DIR__ ) );
+ $classLoader->register();
+ $classLoader = new ClassLoader( 'Proxies', dirname( __DIR__ ) );
+ $classLoader->register();
+ $classLoader = new ClassLoader( 'DoctrineExtensions', \ROOT . 'lib' );
+ $classLoader->register();
+
+ $conn = \Doctrine\DBAL\DriverManager::getConnection( $this->getConnectionOptions(), $this->getConfig(), $this->createEventManager() );
+ parent::__construct( $conn, $this->getConfig(), $conn->getEventManager() );
+
+ self::$em = self::create( $this->getConnectionOptions(), $this->getConfig(), $this->createEventManager() );
+ }
+
+ private function getConnectionOptions()
+ {
+ return array(
+ 'dbname' => self::DATABASE,
+ 'user' => self::USER,
+ 'password' => self::PASS,
+ 'host' => self::HOST,
+ 'driver' => self::DRIVER,
+ 'CHARSET' => self::CHARSET,
+ 'driverOptions' => array( 'CHARSET'=> self::CHARSET )
+ );
+ }
+
+ private function getConfig()
+ {
+ $config = new Configuration;
+
+ // Proxies
+ $config->setProxyDir( dirname( __DIR__ ) . '/Proxies' );
+ $config->setProxyNamespace( 'Proxies' );
+ $config->setAutoGenerateProxyClasses( APPLICATION_ENV === 'development' );
+
+ // Caching
+ $cache = ( \APPLICATION_ENV !== 'development' && extension_loaded( 'apc' ) )
+ ? new \Doctrine\Common\Cache\ApcCache
+ : new \Doctrine\Common\Cache\ArrayCache;
+
+ $config->setMetadataCacheImpl( $cache );
+ $config->setQueryCacheImpl( $cache );
+ $config->setResultCacheImpl( $cache );
+
+ // Annotations
+// $annotationDriver = $config->newDefaultAnnotationDriver( array( \ROOT . 'Application/Entities' ) );
+
+ $reader = new \Doctrine\Common\Annotations\AnnotationReader( $cache, new \Doctrine\Common\Annotations\Parser );
+ $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
+ $annotationDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver( $reader, array( dirname( __DIR__ ) . '/Entities' ) );
+
+// $annotationDriver = new \Doctrine\Common\Annotations\AnnotationReader(
+// $cache, new \Doctrine\Common\Annotations\Parser
+// );
+ $config->setMetadataDriverImpl( $annotationDriver );
+
+ return $config;
+ }
+
+ private function createEventManager()
+ {
+ // Add EventSubscribers here...
+ $evm = new \Doctrine\Common\EventManager();
+
+ return $evm;
+ }
+
+ /**
+ *
+ * @return EntityManager
+ */
+ public function getManager()
+ {
+ return self::$em;
+ }
+
+ public function console()
+ {
+ $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
+ 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper( self::$em->getConnection() ),
+ 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper( self::$em )
+ ));
+
+ \Doctrine\ORM\Tools\Console\ConsoleRunner::run( $helperSet );
+ }
+}
View
185 Application/Module/RedisExample/Controller/CrudController.php
@@ -0,0 +1,185 @@
+<?php
+
+namespace Application\Module\RedisExample\Controller;
+
+use \Insomnia\Controller\Action,
+ \Application\Module\RedisExample\Bootstrap\Doctrine,
+ \Application\Controller\TestController,
+ \Insomnia\Request\Validator\IntegerValidator,
+ \Insomnia\Request\Validator\StringValidator,
+ \Insomnia\Controller\NotFoundException,
+ \Insomnia\Response\Paginator,
+ \Insomnia\Response\Code,
+ \Application\Module\RedisExample\Queries\TestQuery;
+
+use \Insomnia\Kernel\Module\RequestValidator\Request\RequestValidator;
+
+use \Application\Module\RedisExample\DataMapper\Test as DataMapper;
+use \Application\Module\RedisExample\Entities\Test as ExampleEntity;
+
+/**
+ * Test Create Action
+ *
+ * @insomnia:Resource
+ * @insomnia:Route( "/example" )
+ *
+ */
+class RedisController extends Action
+{
+ /**
+ * Create a new Entity
+ *
+ * @insomnia:Route( "/redis", name="test_create" )
+ * @insomnia:Method( "PUT" )
+ * @insomnia:Documentation( category="Redis Demo" )
+ *
+ * @insomnia:Request({
+ * @insomnia:Param( name="name", type="string", minlength="4", maxlength="10" )
+ * })
+ *
+ */
+ public function create()
+ {
+ $test = new ExampleEntity;
+ $test->setName( $this->validator->getParam( 'name' ) );
+
+ $doctrine = new Doctrine;
+ $doctrine->getManager()->persist( $test );
+ $doctrine->getManager()->flush();
+
+ $dataMapper = new DataMapper( $test );
+ $this->response->merge( $dataMapper->export() );
+ $this->response->setCode( Code::HTTP_CREATED );
+ }
+
+ /**
+ * Delete a Entity
+ *
+ * @insomnia:Route( "/redis/:id", name="test_delete" )
+ * @insomnia:Method( "DELETE" )
+ * @insomnia:Documentation( category="Redis Demo" )
+ *
+ * @insomnia:Request({
+ * @insomnia:Param( name="id", type="integer" )
+ * })
+ *
+ */
+ public function delete()
+ {
+ $doctrine = new Doctrine;
+ $test = $doctrine->getManager()->find( 'Application\Module\RedisExample\Entities\Test', $this->validator->getParam( 'id' ) );
+ if( !$test ) throw new NotFoundException( 'Entity Not Found' );
+
+ $doctrine->getManager()->remove( $test );
+ $doctrine->getManager()->flush();
+
+ $this->response[ 'message' ] = 'Entity Deleted';
+ }
+
+ /**
+ * List Entities
+ *
+ * @insomnia:Route( "/redis", name="test_index" )
+ * @insomnia:Method( "GET" )
+ * @insomnia:Documentation( category="Redis Demo" )
+ *
+ * @insomnia:Request({
+ * @insomnia:Param( name="page", type="integer", optional="true" )
+ * })
+ *
+ */
+ public function index()
+ {
+ $doctrine = new Doctrine;
+ $query = new TestQuery( $doctrine->getManager() );
+
+ $queryObject = $query->getQuery();
+ $queryObject->useResultCache( true, 99999 );
+
+ $paginator = new Paginator( $queryObject );
+ $paginator->setCurrentPage( $this->validator->getParam( 'page' ) );
+
+ $tests = $paginator->getItems();
+ if( !$tests ) throw new NotFoundException( 'Entity Not Found', 404 );
+
+ foreach( $tests as $test )
+ {
+ $dataMapper = new DataMapper( $test );
+ $this->response->push( $dataMapper->export() );
+ }
+ }
+
+ /**
+ * View a Single Entity
+ *
+ * @insomnia:Route( "/redis/:id", name="test_read" )
+ * @insomnia:Method( "GET" )
+ * @insomnia:Documentation( category="Redis Demo" )
+ *
+ * @insomnia:Request({
+ * @insomnia:Param( name="id", type="integer" )
+ * })
+ *
+ */
+ public function read()
+ {
+ $doctrine = new Doctrine;
+ $test = $doctrine->getManager()
+ ->createQuery( 'SELECT t FROM Application\Module\RedisExample\Entities\Test t WHERE t.id = :id' )
+ ->useResultCache( true, 99999 )
+ ->setParameter( 'id', $this->validator->getParam( 'id' ) )
+ ->getResult();
+
+ if( !$test ) throw new NotFoundException( 'Entity Not Found' );
+
+ $dataMapper = new DataMapper( reset( $test ) );
+ $this->response->merge( $dataMapper->export() );
+ }
+
+ /**
+ * Update an Entity
+ *
+ * @insomnia:Route( "/redis/:id", name="test_update" )
+ * @insomnia:Method( "POST" )
+ * @insomnia:Documentation( category="Redis Demo" )
+ *
+ * @insomnia:Request({
+ * @insomnia:Param( name="id", type="integer" ),
+ * @insomnia:Param( name="name", type="string", minlength="4", maxlength="10", optional="true" )
+ * })
+ *
+ */
+ public function update()
+ {
+ $doctrine = new Doctrine;
+ $test = $doctrine->getManager()->find( 'Application\Module\RedisExample\Entities\Test', $this->validator->getParam( 'id' ) );
+ if( !$test ) throw new NotFoundException( 'Entity Not Found' );
+
+ $dataMapper = new DataMapper( $test );
+ $dataMapper->import( $this->validator->getParams() );
+ $doctrine->getManager()->persist( $test );
+ $doctrine->getManager()->flush();
+
+ $this->response->merge( $dataMapper->export() );
+ }
+
+ /**
+ * RedisExample Setup Help
+ *
+ * @insomnia:Route( "/redis/setup", name="RedisExample_setup" )
+ * @insomnia:Method( "GET" )
+ *
+ * @insomnia:View( "Application\Module\RedisExample\View\Setup" )
+ * @insomnia:Layout( "Insomnia\Kernel\Module\Mime\View\Layout" )
+ * @insomnia:Documentation( category="Redis Demo" )
+ *
+ */
+ public function setup()
+ {
+ $this->response[ 'setup' ] = array(
+ 'title' => 'Setting up the example',
+ 'description' => 'You must setup your database before running this example',
+ 'example' => file_get_contents( __DIR__ . '../../Scripts/user_create.sql' ),
+ );
+ }
+}
View
13 Application/Module/RedisExample/DataMapper.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Application\Module\RedisExample;
+
+class DataMapper
+{
+ protected $object;
+
+ public function __construct( $object )
+ {
+ $this->object = $object;
+ }
+}
View
21 Application/Module/RedisExample/DataMapper/Test.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Application\Module\RedisExample\DataMapper;
+
+use \Application\Module\RedisExample\DataMapper;
+
+class Test extends DataMapper
+{
+ public function export()
+ {
+ return array(
+ 'id' => $this->object->getId(),
+ 'name' => $this->object->getName()
+ );
+ }
+
+ public function import( array $data )
+ {
+ if( isset( $data['name'] ) ) $this->object->setName( $data[ 'name' ] );
+ }
+}
View
39 Application/Module/RedisExample/Entities/Test.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Application\Module\RedisExample\Entities;
+
+/** @Entity */
+class Test
+{
+ /**
+ * @Id @GeneratedValue
+ * @Column(type="integer")
+ */
+ private $id;
+
+ /**
+ * @Column(type="string",length=50)
+ *
+ * @insomnia:Property({
+ * @insomnia:Validate( name="id", type="integer" ),
+ * @insomnia:Validate( name="name", class="\Insomnia\Request\Validator\StringValidator", minlength="4", maxlength="10", optional="true" )
+ * })
+ */
+ private $name;
+
+ public function getId() {
+ return $this->id;
+ }
+
+ public function setId($id) {
+ $this->id = $id;
+ }
+
+ public function getName() {
+ return $this->name;
+ }
+
+ public function setName($name) {
+ $this->name = $name;
+ }
+}
View
18 Application/Module/RedisExample/Queries/TestQuery.php
@@ -0,0 +1,18 @@
+<?php
+
+namespace Application\Module\RedisExample\Queries;
+
+use \Doctrine\ORM\QueryBuilder,
+ \Doctrine\ORM\EntityManager,
+ \DoctrineExtensions\Paginate\PaginationAdapter;
+
+class TestQuery extends QueryBuilder
+{
+ public function __construct( EntityManager $em )
+ {
+ parent::__construct( $em );
+
+ $this->select('t');
+ $this->from( 'Application\Module\RedisExample\Entities\Test', 't' );
+ }
+}
View
20 Application/Module/RedisExample/Scripts/doctrine.php
@@ -0,0 +1,20 @@
+<?php
+
+use \Doctrine\Common\ClassLoader;
+
+\define( 'ROOT', dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) . \DIRECTORY_SEPARATOR );
+
+require_once \ROOT.'/lib/Doctrine/Common/ClassLoader.php';
+spl_autoload_register( array( new ClassLoader( 'Doctrine', \ROOT . 'lib' ), 'loadClass' ) );
+spl_autoload_register( array( new ClassLoader( 'Insomnia', \ROOT . 'lib' ), 'loadClass' ) );
+spl_autoload_register( array( new ClassLoader( 'Application', \ROOT ), 'loadClass' ) );
+spl_autoload_register( array( new ClassLoader( 'Symfony', \ROOT . 'lib' ), 'loadClass' ) );
+
+// -----------------------------------------
+
+define('APPLICATION_ENV', "development");
+
+// -----------------------------------------
+
+$doctrine = new \Application\Module\RedisExample\Bootstrap\Doctrine;
+$doctrine->console();
View
14 Application/Module/RedisExample/View/Setup.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace Application\Module\RedisExample\View;
+
+use \Insomnia\Kernel\Module\Mime\View;
+
+class Setup extends View
+{
+ public function __construct()
+ {
+ $view = realpath( __DIR__ . \DIRECTORY_SEPARATOR . 'setup' . \DIRECTORY_SEPARATOR . 'setup.php' );
+ $this->setPath( $view );
+ }
+}
View
12 Application/Module/RedisExample/View/setup/setup.php
@@ -0,0 +1,12 @@
+<div class="insomnia">
+ <div class="insomnia-error-header">
+ <h1 class="insomnia-logo">Insomnia</h1>
+ <div class="insomnia-title">
+ <h1 class="error"><?php echo $this['setup']['title']; ?></h1>
+ <h4><?php echo $this['setup']['description']; ?></h4>
+ </div>
+ </div>
+ <div class="insomnia-documentation">
+ <pre style="padding:20px; border:solid 1px #ddd; background-color: #fff;"><?php echo $this['setup']['example']; ?></pre>
+ </div>
+</div>
Please sign in to comment.
Something went wrong with that request. Please try again.