Skip to content

Instantly share code, notes, and snippets.

@pedrorvidal
Created January 28, 2016 11:24
Show Gist options
  • Select an option

  • Save pedrorvidal/29db8ac30bec931c7e56 to your computer and use it in GitHub Desktop.

Select an option

Save pedrorvidal/29db8ac30bec931c7e56 to your computer and use it in GitHub Desktop.
Classe container
<?php
namespace Container;
use Zend\Session\SessionManager;
use Pimple;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream;
/**
* Classe que armazena todas as dependências para serem chamados na classe Module
*
* @author Praxedes
*/
class Container {
/**
*
* @var Pimple
*/
protected $container;
/**
*
* @var \Doctrine\ORM\EntityManager
*/
protected $em = null;
/**
*
* @param Pimple $pimple
*/
public function __construct(Pimple $pimple) {
//Adiciona o contaneir
$this->container = $pimple;
$factory = new \Base\Doctrine\EntityManagerFactory();
//Instancia o entity manager
$this->container['Doctrine\ORM\EntityManager'] = $factory->getEntityManger();
$this->container['em2'] = $factory->getEntityManager2();
//Adiciona as depenências
$this->getBaseFunctions();
// $this->getEm();
$this->getZendLibrary();
$this->getForms();
$this->getRepositories();
$this->getServices();
$this->getBaseServices();
$this->getAuthServices();
}
/**
*
* @return Pimple
*/
public function getContainer() {
return $this->container;
}
/**
* Instancia o EntityManager
*/
/* public function getEm() {
//Recebe o em
$this->container['Doctrine\ORM\EntityManager'] = function($this) {
if (is_null(self::getEntityManager())) {
$factory = new \Base\Doctrine\EntityManagerFactory();
self::setEm($factory->getEntityManger());
}
return self::getEntityManager();
};
}
*/
/**
* Instancia as bibliotecas do zf2
*/
public function getZendLibrary() {
//Instancia a classe de requisição
$this->container['Zend\Http\PhpEnvironment\Request'] = function () {
return new \Zend\Http\PhpEnvironment\Request();
};
}
/**
*
* Retorna as instâncias dos repositórios
* @return array
*/
public function getForms() {
$this->container['SISGEOF\Form\CrudSimples'] = function($sm) {
$inputFilter = new \SISGEOF\InputFilter\CrudSimples();
return new \SISGEOF\Form\CrudSimples($inputFilter);
};
$this->container['SISGEOF\Form\CrudComplexo'] = function($sm) {
$repository = $sm['SISGEOF\Repository\CrudSimples'];
$repository->setLogger($sm['Base\Log\Log']);
$inputFilter = new \SISGEOF\InputFilter\CrudComplexo();
return new \SISGEOF\Form\CrudComplexo($inputFilter, $repository->getArraySelect(), new \Base\Helper\DatetimeFormat());
};
$this->container['SISGEOF\Form\CrudSimplesFiltro'] = function($sm) {
return new \SISGEOF\Form\CrudSimplesFiltro();
};
}
/**
* Método que configura as dependências
* @return array
*/
public function getAuthServices() {
$this->container['SISGEOF\Auth\Adapter'] = function($sm) {
return new \SISGEOF\Auth\Adapter();
};
}
/**
*
* Retorna os services com as instâncias dos repositories
* @return array
*/
public function getRepositories() {
$this->container['SISGEOF\Repository\CrudSimples'] = function($sm) {
$repository = $sm['Doctrine\ORM\EntityManager']->getRepository('SISGEOF\Entity\CrudSimples');
$repository->setLogger($sm['Base\Log\Log']);
return $repository;
};
$this->container['SISGEOF\Repository\CrudComplexo'] = function($sm) {
$repository = $sm['Doctrine\ORM\EntityManager']->getRepository('SISGEOF\Entity\CrudComplexo');
$repository->setLogger($sm['Base\Log\Log']);
return $repository;
};
$this->container['SISGEOF\Repository\TextoPadrao'] = function($sm) {
$repository = $sm['Doctrine\ORM\EntityManager']->getRepository('SISGEOF\Entity\TextoPadrao');
$repository->setLogger($sm['Base\Log\Log']);
return $repository;
};
}
/**
*
* Retorna as instâncias de services
* @return array
*/
public function getServices() {
$this->container['SISGEOF\Service\CrudSimples'] = function($sm) {
$service = new \SISGEOF\Service\CrudSimples($sm['Doctrine\ORM\EntityManager'], new \SISGEOF\Entity\CrudSimples(), $sm['Base\Log\Log']);
$service->setDadosUser($sm['DadosUser'])
->setLogAlteracao($sm['SISGEOF\Service\LogAlteracao'])
->setDateTimeFormat(new \Base\Helper\DatetimeFormat());
return $service;
};
$this->container['SISGEOF\Service\CrudComplexo'] = function($sm) {
$service = new \SISGEOF\Service\CrudComplexo($sm['Doctrine\ORM\EntityManager'], new \SISGEOF\Entity\CrudComplexo(), $sm['Base\Log\Log']);
$service->setDadosUser($sm['DadosUser'])
->setLogAlteracao($sm['SISGEOF\Service\LogAlteracao'])
->setDateTimeFormat(new \Base\Helper\DatetimeFormat())
->setConfig($sm['config']);
return $service;
};
$this->container['SISGEOF\Service\TextoPadrao'] = function($sm) {
$service = new \SISGEOF\Service\TextoPadrao($sm['Doctrine\ORM\EntityManager'], new \SISGEOF\Entity\TextoPadrao(), $sm['Base\Log\Log']);
$service->setDadosUser($sm['DadosUser'])
->setLogAlteracao($sm['SISGEOF\Service\LogAlteracao'])
->setDateTimeFormat(new \Base\Helper\DatetimeFormat())
->setConfig($sm['config']);
return $service;
};
$this->container['SISGEOF\Service\LogAlteracao'] = function($sm) {
return new \SISGEOF\Service\LogAlteracao($sm['Doctrine\ORM\EntityManager'], new \SISGEOF\Entity\LogAlteracao(), $sm['Base\Log\Log']);
};
}
/**
*
* Retorna os serviços básicos
* @return array
*/
public function getBaseServices() {
$this->container['SessionManager'] = function($sm) {
$manager = new SessionManager();
$manager->rememberMe(72000);
return $manager;
};
$this->container['DadosUser'] = function($sm) {
//Busca e retorna os dados do usuário logado armazenado na sessão
$sessionStorage = new \Zend\Authentication\Storage\Session('UserSession');
return $sessionStorage->read();
};
}
public function getBaseFunctions() {
$this->container['config'] = function($sm) {
$config = include __DIR__ . "/../../config/general.php";
return $config;
};
$this->container['Base\Log\Log'] = function($sm) {
//Busca o config
$config = $sm['config'];
//Instancia o log e stream
$logger = new Logger;
$writer = new Stream($config['basePath'].$config['log']);
//Adiciona o writer e retorna o log
$logger->addWriter($writer);
return $logger;
};
$this->container['Mail'] = function($sm) {
//Busca as configurações
$config = $sm['config'];
//Cria o objeto de transporte
$transport = new \Zend\Mail\Transport\Smtp();
$options = new \Zend\Mail\Transport\SmtpOptions($config['mail']);
$transport->setOptions($options);
//Instancia o mail
return new \Base\Mail\Mail($transport, new \Zend\Mail\Message());
};
$this->container['HelpersView'] = function($sm) {
$configView = include __DIR__ . "/../../config/view.php";
return $configView;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment