PHP code example of programmingarehard / resource-bundle
1. Go to this page and download the library: Download programmingarehard/resource-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
programmingarehard / resource-bundle example snippets
// src/MyApp/CoreBundle/Entity/Task.php
namespace MyApp\CoreBundle\Entity;
use MyApp\CoreBundle\Domain\Task\TaskInterface;
use ProgrammingAreHard\ResourceBundle\Domain\ResourceInterface;
class Task implements TaskInterface, ResourceInterface
{
protected $id;
protected $task;
public function getId()
{
return $this->id;
}
public function isNew()
{
return null === $this->getId();
}
public function getTask()
{
return $this->task;
}
public function setTask($task)
{
$this->task = $task;
}
}
// src/MyApp/CoreBundle/Domain/Task/Repository/Doctrine/TaskRepository.php
namespace MyApp\CoreBundle\Domain\Task\Repository\Doctrine;
use MyApp\CoreBundle\Domain\Task\Repository\TaskRepositoryInterface;
use MyApp\CoreBundle\Entity\Task;
use ProgrammingAreHard\ResourceBundle\Domain\Repository\Doctrine\BaseResourceRepository;
class TaskRepository extends BaseResourceRepository implements TaskRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function newInstance()
{
return new Task;
}
}
// src/MyApp/CoreBundle/Domain/Task/Form/Type/TaskType.php
namespace MyApp\CoreBundle\Domain\Task\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class TaskType extends AbstractType
{
private $class;
public function __construct($class)
{
$this->class = $class;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('task');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => $this->class,
));
}
public function getName()
{
return 'task';
}
}
// src/MyApp/CoreBundle/Controller/TaskController.php
namespace MyApp\CoreBundle\Controller;
use MyApp\CoreBundle\Entity\Task;
use ProgrammingAreHard\ResourceBundle\Controller\ResourceController;
use ProgrammingAreHard\ResourceBundle\Domain\Event\ResourceEvents;
use ProgrammingAreHard\ResourceBundle\Domain\ResourceInterface;
class TaskController extends ResourceController
{
/**
* Task class.
*
* @var string
*/
protected $resourceClass = Task::CLASS; // using php 5.5's class constant
/**
* Show current user's tasks.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction()
{
$tasks = $this->getUser()->getTasks();
foreach ($tasks as $task) {
$this->getResourceEventDispatcher()->dispatch(ResourceEvents::PRE_VIEW, $task);
}
return $this
->setData([$this->getPluralizedResourceName() => $tasks])
->respond();
}
/**
* {@inheritdoc}
*/
protected function getResourceLocation(ResourceInterface $resource)
{
return $this->generateUrl('my_app_task_view', ['id' => $resource->getId()]);
}
/**
* {@inheritdoc}
*/
protected function getFormProcessor()
{
return $this->get('myapp.resource.form_processor');
}
/**
* {@inheritdoc}
*/
protected function getResourceManager()
{
return $this->get('myapp.resource.manager');
}
/**
* {@inheritdoc}
*/
protected function getResourceRepository()
{
return $this->get('myapp.task.repository');
}
}
//src/MyApp/CoreBundle/Domain/Resource/Security/Voter/ResourceVoter.php
namespace MyApp\CoreBundle\Domain\Resource\Security\Voter;
use ProgrammingAreHard\ResourceBundle\Domain\ResourceInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class ResourceVoter implements VoterInterface
{
/**
* {@inheritdoc}
*/
public function supportsAttribute($attribute)
{
return true
}
/**
* {@inheritdoc}
*/
public function supportsClass($class)
{
return true
}
/**
* {@inheritdoc}
*/
public function vote(TokenInterface $token, $resource, array $attributes)
{
// Your application's logic to determine if the user has permission
// to perform 'VIEW', 'CREATE', 'UPDATE', and/or 'DELETE' permissions.
if ($resource instanceof ResourceInterface) {
return VoterInterface::ACCESS_GRANTED;
}
return VoterInterface::ACCESS_ABSTAIN;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.