1
0
mirror of https://gitnet.fr/deblan/gist.git synced 2021-08-14 08:30:49 +02:00

PHP Documentation

This commit is contained in:
Simon Vieille 2016-11-13 00:44:23 +01:00
parent aa73578efe
commit b0f74d8dc8
32 changed files with 643 additions and 82 deletions

View File

@ -5,14 +5,34 @@ namespace Gist\Api;
use GuzzleHttp\Client as BaseClient;
/**
* Class Client
* Class Client.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class Client extends BaseClient
{
/**
* URI of creation
*
* @const string
*/
const CREATE = '/en/api/create';
/**
* URI of updating
*
* @const string
*/
const UPDATE = '/en/api/update/{gist}';
/**
* Creates a gist
*
* @param string $title The title
* @param string $type The type
* @param string $content The content
* @return array
*/
public function create($title, $type, $content)
{
$response = $this->post(
@ -34,9 +54,16 @@ class Client extends BaseClient
return [];
}
/**
* Clones and update a gist
*
* @param string $gist Gist's ID
* @param string $content The content
*
* @return array
*/
public function update($gist, $content)
{
$response = $this->post(
str_replace('{gist}', $gist, self::UPDATE),
array(

View File

@ -5,16 +5,23 @@ namespace Gist;
use Silex\Application as SilexApplication;
/**
* @deprecated The static version should be avoided, use DI instead.
* @deprecated The static version should be avoided, use DI instead
*/
class Application extends SilexApplication
{
/**
* Creates an instance of Application.
*
* @static
*
* @return Application
*/
public static function getInstance()
{
static $app;
if (null === $app) {
$app = new static;
$app = new static();
}
return $app;

View File

@ -8,8 +8,16 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
/**
* class CreateCommand.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class CreateCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$types = implode(', ', $this->getTypes());
@ -45,6 +53,9 @@ EOF
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
//$output->writeln(sprintf('<comment>%s</comment> bar.', 'test'));
@ -88,7 +99,7 @@ EOF
return true;
}
if ($input->getOption('show-id')) {
$output->writeln($gist['gist']['Id']);
@ -98,6 +109,11 @@ EOF
$output->writeln(json_encode($gist));
}
/**
* Returns the list of types.
*
* @return array
*/
protected function getTypes()
{
$types = array(

View File

@ -10,8 +10,16 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\Table;
use GitWrapper\GitException;
/**
* class StatsCommand;
*
* @author Simon Vieille <simon@deblan.fr>
*/
class StatsCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
@ -22,7 +30,10 @@ Show stats about GIST
EOF
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$gistService = $this->getSilexApplication()['gist'];

View File

@ -8,8 +8,16 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
/**
* class UpdateCommand.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class UpdateCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$types = implode(', ', $this->getTypes());
@ -44,6 +52,9 @@ EOF
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
//$output->writeln(sprintf('<comment>%s</comment> bar.', 'test'));
@ -80,7 +91,7 @@ EOF
return true;
}
if ($input->getOption('show-id')) {
$output->writeln($gist['gist']['Id']);
@ -90,6 +101,11 @@ EOF
$output->writeln(json_encode($gist));
}
/**
* Returns the list of types.
*
* @return array
*/
protected function getTypes()
{
$types = array(

View File

@ -7,16 +7,27 @@ use Symfony\Component\Console\Output\OutputInterface;
use Knp\Command\Command;
use Symfony\Component\Console\Question\Question;
/**
* Class UserCreateCommand.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class UserCreateCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('user:create')
->setDescription('Create a user')
->setHelp("");
->setHelp('');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
@ -28,7 +39,7 @@ class UserCreateCommand extends Command
while (trim($username) === '') {
$question = new Question('Username: ', '');
$username = $helper->ask($input, $output, $question);
if ($userProvider->userExists($username)) {
$output->writeln('<error>This username is already used.</error>');
$username = '';

View File

@ -10,7 +10,8 @@ use Gist\Model\GistQuery;
use Gist\Form\ApiUpdateGistForm;
/**
* Class ApiController
* Class ApiController.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ApiController extends Controller

View File

@ -9,23 +9,42 @@ use Gist\Model\GistQuery;
use Symfony\Component\HttpFoundation\Response;
/**
* Class Controller
* Class Controller.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class Controller
{
/**
* @var Application
*/
protected $app;
/**
* __construct.
*
* @param Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* Returns the application.
*
* @return Application
*/
public function getApp()
{
return $this->app;
}
/**
* Returns a 404 response.
*
* @return Response
*/
protected function notFoundResponse()
{
$app = $this->getApp();
@ -38,7 +57,16 @@ class Controller
404
);
}
/**
* Returns the default options of a gist view.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit ID
*
* @return array
*/
protected function getViewOptions(Request $request, $gist, $commit)
{
$app = $this->getApp();
@ -67,6 +95,13 @@ class Controller
);
}
/**
* Returns the content of the gist depending of the commit and its history.
*
* @param Gist $gist
* @param mixed $commit
* @param mixed $history
*/
protected function getContentByCommit(Gist $gist, &$commit, $history)
{
$app = $this->getApp();
@ -90,6 +125,11 @@ class Controller
return $app['gist']->getContent($gist, $commit);
}
/**
* Returns the connected user.
*
* @return mixed
*/
public function getUser()
{
$app = $this->getApp();
@ -110,6 +150,14 @@ class Controller
return $user;
}
/**
* Renders a view.
*
* @param string $template
* @param array $params
*
* @return string
*/
public function render($template, array $params = null)
{
$app = $this->getApp();

View File

@ -10,11 +10,19 @@ use GitWrapper\GitException;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Class EditController
* Class EditController.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class EditController extends Controller
{
/**
* Creation page.
*
* @param Request $request
*
* @return string
*/
public function createAction(Request $request)
{
$app = $this->getApp();
@ -44,6 +52,13 @@ class EditController extends Controller
);
}
/**
* Cloning page.
*
* @param Request $request
*
* @return string
*/
public function cloneAction(Request $request, $gist, $commit)
{
$app = $this->getApp();

View File

@ -7,15 +7,21 @@ use Gist\Model\User;
use Gist\Form\UserRegisterForm;
use Gist\Form\UserLoginForm;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Class LoginController
* Class LoginController.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class LoginController extends Controller
{
/**
* Registration page.
*
* @param Request $request
*
* @return string
*/
public function registerAction(Request $request)
{
$app = $this->getApp();
@ -54,13 +60,20 @@ class LoginController extends Controller
return $this->render(
'Login/register.html.twig',
[
'form' => $form->createView(),
'error' => isset($error) ? $error : '',
'form' => $form->createView(),
'error' => isset($error) ? $error : '',
'success' => isset($success) ? $success : '',
]
);
}
/**
* Login page.
*
* @param Request $request
*
* @return string
*/
public function loginAction(Request $request)
{
$app = $this->getApp();
@ -87,7 +100,7 @@ class LoginController extends Controller
return $this->render(
'Login/login.html.twig',
[
'form' => $form->createView(),
'form' => $form->createView(),
'error' => isset($error) ? $error : '',
]
);

View File

@ -3,26 +3,34 @@
namespace Gist\Controller;
use Symfony\Component\HttpFoundation\Request;
use Gist\Model\GistQuery;
use Gist\Form\DeleteGistForm;
use Gist\Form\FilterGistForm;
/**
* Class MyController
* Class MyController.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class MyController extends Controller
{
/**
* "My" page.
*
* @param Request $request
* @param int $page
*
* @return string
*/
public function myAction(Request $request, $page)
{
$page = (int) $page;
$page = (int) $page;
$app = $this->getApp();
$deleteForm = new DeleteGistForm($app['form.factory'], $app['translator']);
$deleteForm = $deleteForm->build()->getForm();
$options = array(
'type' => 'all',
'type' => 'all',
'cipher' => 'anyway',
);
@ -42,7 +50,7 @@ class MyController extends Controller
$options = $filterForm->getData();
}
}
$gists = $this->getUser()->getGistsPager($page, $options);
if ($request->isMethod('post')) {
@ -64,11 +72,11 @@ class MyController extends Controller
return $this->render(
'My/my.html.twig',
array(
'gists' => $gists,
'page' => $page,
'deleteForm' => $deleteForm->createView(),
'filterForm' => $filterForm->createView(),
'deleted' => !empty($deleted),
'gists' => $gists,
'page' => $page,
'deleteForm' => $deleteForm->createView(),
'filterForm' => $filterForm->createView(),
'deleted' => !empty($deleted),
)
);
}

View File

@ -9,11 +9,21 @@ use Gist\Model\Gist;
use Symfony\Component\HttpFoundation\Response;
/**
* Class ViewController
* Class ViewController.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ViewController extends Controller
{
/**
* View action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
* @return string|Response
*/
public function viewAction(Request $request, $gist, $commit)
{
$app = $this->getApp();
@ -27,6 +37,15 @@ class ViewController extends Controller
}
}
/**
* Embed action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
* @return string|Response
*/
public function embedAction(Request $request, $gist, $commit)
{
$app = $this->getApp();
@ -40,6 +59,15 @@ class ViewController extends Controller
}
}
/**
* JS embed action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
* @return string|Response
*/
public function embedJsAction(Request $request, $gist, $commit)
{
$viewOptions = $this->getViewOptions($request, $gist, $commit);
@ -53,6 +81,15 @@ class ViewController extends Controller
);
}
/**
* Raw action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
* @return string|Response
*/
public function rawAction(Request $request, $gist, $commit)
{
$viewOptions = $this->getViewOptions($request, $gist, $commit);
@ -70,6 +107,15 @@ class ViewController extends Controller
}
}
/**
* Download action.
*
* @param Request $request
* @param string $gist Gist's ID
* @param string $commit The commit
*
* @return string|Response
*/
public function downloadAction(Request $request, $gist, $commit)
{
$app = $this->getApp();
@ -94,6 +140,14 @@ class ViewController extends Controller
}
}
/**
* Revisions action.
*
* @param Request $request
* @param string $gist Gist's ID
*
* @return string|Response
*/
public function revisionsAction(Request $request, $gist)
{
$app = $this->getApp();

View File

@ -3,17 +3,23 @@
namespace Gist;
use Silex\ControllerResolver as BaseControllerResolver;
use Gist\Application;
/**
* Class DecoratorControllerResolver
* Class DecoratorControllerResolver.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ControllerResolver extends BaseControllerResolver
{
/**
* Instanciates a controller.
*
* @param string $class
*
* @return Gist\Controller
*/
protected function instantiateController($class)
{
return new $class($this->app);
}
}

View File

@ -6,15 +6,34 @@ use Symfony\Component\Form\FormFactory;
use Symfony\Component\Translation\Translator;
/**
* Class AbstractForm
* Class AbstractForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
abstract class AbstractForm
{
/**
* The builder.
*
* @var Symfony\Component\Form\FormBuilder
*/
protected $builder;
/**
* The translator.
*
* @var Translator
*/
protected $translator;
/**
* __construct.
*
* @param FormFactory $formFactory
* @param Translator $translator
* @param mixed $data
* @param array $formFactoryOptions
*/
public function __construct(FormFactory $formFactory, Translator $translator, $data = null, $formFactoryOptions = array())
{
$this->translator = $translator;
@ -22,15 +41,32 @@ abstract class AbstractForm
$this->builder = $formFactory->createNamedBuilder($this->getName(), 'form', $data, $formFactoryOptions);
}
/**
* Returns the form from the builder.
*
* @return Symfony\Component\Form\Form
*/
public function getForm()
{
return $this->builder->getForm();
}
/**
* Returns the form's name.
*
* @return string
*/
public function getName()
{
return 'form';
}
/**
* Builds the form.
*
* @param array $options
*
* @return Symfony\Component\Form\FormBuilder
*/
abstract public function build(array $options = array());
}

View File

@ -3,11 +3,15 @@
namespace Gist\Form;
/**
* Class ApiCreateGistForm
* Class ApiCreateGistForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ApiCreateGistForm extends CreateGistForm
{
/**
* {@inheritdoc}
*/
public function build(array $options = array())
{
parent::build($options);

View File

@ -3,11 +3,15 @@
namespace Gist\Form;
/**
* Class ApiUpdateGistForm
* Class ApiUpdateGistForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ApiUpdateGistForm extends ApiCreateGistForm
{
/**
* {@inheritdoc}
*/
public function build(array $options = array())
{
parent::build($options);

View File

@ -3,11 +3,15 @@
namespace Gist\Form;
/**
* Class CreateGistForm
* Class CreateGistForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class CloneGistForm extends CreateGistForm
{
/**
* {@inheritdoc}
*/
public function build(array $options = array())
{
parent::build($options);

View File

@ -5,11 +5,15 @@ namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class CreateGistForm
* Class CreateGistForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class CreateGistForm extends AbstractForm
{
/**
* {@inheritdoc}
*/
public function build(array $options = array())
{
$this->builder->add(
@ -69,6 +73,11 @@ class CreateGistForm extends AbstractForm
return $this->builder;
}
/**
* Returns the types for generating the form.
*
* @return array
*/
protected function getTypes()
{
$types = array(
@ -78,7 +87,7 @@ class CreateGistForm extends AbstractForm
'php' => '',
'sql' => '',
'xml' => '',
'yaml'=> '',
'yaml' => '',
'perl' => '',
'c' => '',
'asp' => '',

View File

@ -5,11 +5,15 @@ namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class DeleteGistForm
* Class DeleteGistForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class DeleteGistForm extends AbstractForm
{
/**
* {@inheritdoc}
*/
public function build(array $options = array())
{
$this->builder->add(
@ -22,12 +26,15 @@ class DeleteGistForm extends AbstractForm
),
)
);
$this->builder->setMethod('POST');
return $this->builder;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'delete';

View File

@ -5,11 +5,15 @@ namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class CreateGistForm
* Class CreateGistForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class FilterGistForm extends AbstractForm
{
/**
* {@inheritdoc}
*/
public function build(array $options = array())
{
$this->builder->add(
@ -45,6 +49,11 @@ class FilterGistForm extends AbstractForm
return $this->builder;
}
/**
* Returns the types for generating the form.
*
* @return array
*/
protected function getTypes()
{
$types = array(
@ -55,7 +64,7 @@ class FilterGistForm extends AbstractForm
'php' => '',
'sql' => '',
'xml' => '',
'yaml'=> '',
'yaml' => '',
'perl' => '',
'c' => '',
'asp' => '',
@ -72,6 +81,9 @@ class FilterGistForm extends AbstractForm
return $types;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'filter';

View File

@ -5,11 +5,15 @@ namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class UserLoginForm
* Class UserLoginForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class UserLoginForm extends AbstractForm
{
/**
* {@inheritdoc}
*/
public function build(array $options = array())
{
$this->builder->add(
@ -45,7 +49,7 @@ class UserLoginForm extends AbstractForm
),
)
);
$this->builder->add(
'_remember_me',
'checkbox',
@ -63,6 +67,9 @@ class UserLoginForm extends AbstractForm
return $this->builder;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return '';

View File

@ -5,11 +5,15 @@ namespace Gist\Form;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class UserRegisterForm
* Class UserRegisterForm.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class UserRegisterForm extends AbstractForm
{
/**
* {@inheritdoc}
*/
public function build(array $options = array())
{
$this->builder->add(

View File

@ -4,8 +4,20 @@ namespace Gist\Model;
use Gist\Model\Base\Gist as BaseGist;
/**
* Class Gist.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class Gist extends BaseGist
{
/**
* Hydrates the gist with array data.
*
* @param array $data
*
* @return Gist
*/
public function hydrateWith(array $data)
{
if (isset($data['title'])) {
@ -21,11 +33,21 @@ class Gist extends BaseGist
return $this;
}
/**
* Generates a unique filename.
*
* @return string
*/
public function generateFilename()
{
$this->setFile(uniqid());
}
/**
* Returns the type for Geshi.
*
* @return string
*/
public function getGeshiType()
{
$data = array(
@ -35,6 +57,11 @@ class Gist extends BaseGist
return str_replace(array_keys($data), array_values($data), $this->getType());
}
/**
* Returns the extension depending of the type.
*
* @return string
*/
public function getTypeAsExtension()
{
$data = array(

View File

@ -5,14 +5,9 @@ namespace Gist\Model;
use Gist\Model\Base\GistQuery as BaseGistQuery;
/**
* Skeleton subclass for performing query and update operations on the 'gist' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
* Class GistQuery.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class GistQuery extends BaseGistQuery
{

View File

@ -6,18 +6,36 @@ use Gist\Model\Base\User as BaseUser;
use Symfony\Component\Security\Core\User\UserInterface;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Connection\ConnectionInterface;
.
/**
* Class User.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class User extends BaseUser implements UserInterface
{
/**
* Erases credentials.
*
* @return void
*/
public function eraseCredentials()
{
}
/**
* Returns roles.
*
* @return array
*/
public function getRoles()
{
return explode(',', parent::getRoles());
}
/**
* {@inheritdoc}
*/
public function getGists(Criteria $criteria = null, ConnectionInterface $con = null)
{
if ($criteria === null) {
@ -27,6 +45,15 @@ class User extends BaseUser implements UserInterface
return parent::getGists($criteria, $con);
}
/**
* Generates a pager of the user's gists.
*
* @param int $page
* @param array $options
* @param int $maxPerPage
*
* @return Propel\Runtime\Util\PropelModelPager
*/
public function getGistsPager($page, $options = array(), $maxPerPage = 10)
{
$query = GistQuery::create()

View File

@ -5,14 +5,9 @@ namespace Gist\Model;
use Gist\Model\Base\UserQuery as BaseUserQuery;
/**
* Skeleton subclass for performing query and update operations on the 'user' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
* Class UserQuery.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class UserQuery extends BaseUserQuery
{

View File

@ -7,36 +7,48 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\HttpFoundation\Request;
/**
* Class AuthenticationListener
* Class AuthenticationListener.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class AuthenticationListener implements ListenerInterface
{
/**
* @var TokenStorageInterface
*/
protected $tokenStorage;
/**
* @var AuthenticationManagerInterface
*/
protected $authenticationManager;
/**
* __construct.
*
* @param TokenStorageInterface $tokenStorage
* @param AuthenticationManagerInterface $authenticationManager
*/
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager)
{
$this->tokenStorage = $tokenStorage;
$this->authenticationManager = $authenticationManager;
}
/**
* @param GetResponseEvent $event
*/
public function handle(GetResponseEvent $event)
{
$request = $event->getRequest();
$request = $event->getRequest();
$username = $request->get('_username');
$password = $request->get('_password');
if (!empty($username)) {
$token = new UsernamePasswordToken($username, $password, 'default');
try {
$authToken = $this->authenticationManager->authenticate($token);
$this->tokenStorage->setToken($token);
@ -46,7 +58,7 @@ class AuthenticationListener implements ListenerInterface
$this->tokenStorage->setToken(null);
return;
}
}
}
}
}

View File

@ -9,18 +9,32 @@ use Gist\Service\UserProvider;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
/**
* Class AuthenticationProvider
* Class AuthenticationProvider.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class AuthenticationProvider implements AuthenticationProviderInterface
{
/**
* @var UserProvider
*/
protected $userProvider;
/**
* __construct.
*
* @param UserProvider $userProvider
*/
public function __construct(UserProvider $userProvider)
{
$this->userProvider = $userProvider;
}
/**
* Authenticates.
*
* @param TokenInterface $token
*/
public function authenticate(TokenInterface $token)
{
$user = $this->userProvider->loadUserByUsername($token->getUser());
@ -29,7 +43,7 @@ class AuthenticationProvider implements AuthenticationProviderInterface
$isValid = $this->userProvider->getEncoder()->isPasswordValid(
$user->getPassword(),
$token->getCredentials(),
$user->getSalt()
$user->getSalt()
);
if (!$isValid) {
@ -42,6 +56,13 @@ class AuthenticationProvider implements AuthenticationProviderInterface
throw new AuthenticationException('Authentication failed.');
}
/**
* Returns if the token instance is supported.
*
* @param TokenInterface $token
*
* @return bool
*/
public function supports(TokenInterface $token)
{
return $token instanceof UsernamePasswordToken;

View File

@ -7,11 +7,17 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Class LogoutSuccessHandler
* Class LogoutSuccessHandler.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class LogoutSuccessHandler implements LogoutSuccessHandlerInterface
{
/**
* @param Request $request
*
* @return RedirectResponse
*/
public function onLogoutSuccess(Request $request)
{
$targetUrl = $request->query->get('target_url') ? $request->query->get('target_url') : '/';
@ -19,4 +25,3 @@ class LogoutSuccessHandler implements LogoutSuccessHandlerInterface
return new RedirectResponse($targetUrl);
}
}

View File

@ -11,19 +11,40 @@ use Gist\Model\GistQuery;
use Gist\Model\User;
/**
* Class Gist
* Class Gist.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class Gist
{
/**
* @var string
*/
protected $gistPath;
/**
* @var GitWrapper
*/
protected $gitWrapper;
/**
* @var GitWorkingCopy
*/
protected $gitWorkingCopy;
/**
* @var GeSHi
*/
protected $geshi;
/**
* __construct.
*
* @param mixed $gistPath
* @param GitWrapper $gitWrapper
* @param GitWorkingCopy $gitWorkingCopy
* @param GeSHi $geshi
*/
public function __construct($gistPath, GitWrapper $gitWrapper, GitWorkingCopy $gitWorkingCopy, GeSHi $geshi)
{
$this->gistPath = $gistPath;
@ -32,11 +53,23 @@ class Gist
$this->geshi = $geshi;
}
/**
* Returns a collection of gists.
*
* @return Propel\Runtime\Collection\ObjectCollection
*/
public function getGists()
{
return GistQuery::create()->find();
}
/**
* Returns the history of a Gist.
*
* @param GistModel $gist
*
* @return array
*/
public function getHistory(GistModel $gist)
{
$command = GitCommand::getInstance('log', '--format=medium', $gist->getFile());
@ -49,7 +82,7 @@ class Gist
$history = [];
for ($i = count($commits) - 1; $i >= 0; $i--) {
for ($i = count($commits) - 1; $i >= 0; --$i) {
$commit = trim($commits[$i][1]);
$command = GitCommand::getInstance('show', '--no-color', $commit);
@ -75,6 +108,14 @@ class Gist
return $history;
}
/**
* Returns the content of a gist.
*
* @param GistModel $gist
* @param string $commit
*
* @return string
*/
public function getContent(GistModel $gist, $commit)
{
$command = GitCommand::getInstance('cat-file', '-p', $commit.':'.$gist->getFile());
@ -84,6 +125,15 @@ class Gist
return str_replace("\r\n", "\n", $this->gitWrapper->run($command));
}
/**
* Creates a gist.
*
* @param GistModel $gist
* @param array $data
* @param mixed $user
*
* @return GistModel
*/
public function create(GistModel $gist, array $data, $user = null)
{
$gist->hydrateWith($data);
@ -104,6 +154,14 @@ class Gist
return $gist;
}
/**
* Makes a commit.
*
* @param GistModel $gist
* @param array $data
*
* @return GistModel
*/
public function commit(GistModel $gist, array $data)
{
file_put_contents($this->gistPath.'/'.$gist->getFile(), $data['content']);
@ -115,6 +173,14 @@ class Gist
return $gist;
}
/**
* Highlight the content.
*
* @param string $type
* @param string $content
*
* @return string
*/
public function highlight($type, $content)
{
$this->geshi->set_source($content);

View File

@ -5,11 +5,19 @@ namespace Gist\Service;
use InvalidArgumentException;
/**
* Class SaltGenerator
* Class SaltGenerator.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class SaltGenerator
{
/**
* Generates a random salt.
*
* @param int $length
*
* @return string
*/
public function generate($length = 32)
{
if (!is_numeric($length)) {

View File

@ -9,24 +9,43 @@ use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Gist\Service\SaltGenerator;
/**
* Class UserProvider
* Class UserProvider.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class UserProvider implements UserProviderInterface
{
/**
* @var MessageDigestPasswordEncoder
*/
protected $encoder;
/**
* @var SaltGenerator
*/
protected $saltGenerator;
/**
* __construct.
*
* @param MessageDigestPasswordEncoder $encoder
* @param SaltGenerator $saltGenerator
*/
public function __construct(MessageDigestPasswordEncoder $encoder, SaltGenerator $saltGenerator)
{
$this->encoder = $encoder;
$this->saltGenerator = $saltGenerator;
}
/**
* Setter of encoder.
*
* @param MessageDigestPasswordEncoder $encoder
*
* @return UserProvider
*/
public function setEncoder(MessageDigestPasswordEncoder $encoder)
{
$this->encoder = $encoder;
@ -34,11 +53,23 @@ class UserProvider implements UserProviderInterface
return $this;
}
/**
* Getter of encoder.
*
* @return MessageDigestPasswordEncoder
*/
public function getEncoder()
{
return $this->encoder;
}
/**
* Setter of saltGenerator.
*
* @param SaltGenerator $saltGenerator
*
* @return UserProvider
*/
public function setSaltGenerator(SaltGenerator $saltGenerator)
{
$this->saltGenerator = $saltGenerator;
@ -46,11 +77,23 @@ class UserProvider implements UserProviderInterface
return $this;
}
/**
* Getter of saltGenerator.
*
* @return SaltGenerator
*/
public function getSaltGenerator()
{
return $this->saltGenerator;
}
/**
* Checks if the given username is a user.
*
* @param string $username
*
* @return bool
*/
public function userExists($username)
{
return UserQuery::create()
@ -58,11 +101,24 @@ class UserProvider implements UserProviderInterface
->count() > 0;
}
/**
* Creates a User.
*
* @return User
*/
public function createUser()
{
return new User();
}
/**
* Registers an user.
*
* @param User $user
* @param string $password
*
* @return User
*/
public function registerUser(User $user, $password)
{
$user->setSalt($this->saltGenerator->generate());
@ -75,6 +131,14 @@ class UserProvider implements UserProviderInterface
return $user;
}
/**
* Updates an user.
*
* @param User $user
* @param string $password
*
* @return User
*/
public function updateUserPassword(User $user, $password)
{
$user
@ -84,6 +148,13 @@ class UserProvider implements UserProviderInterface
return $user;
}
/**
* Loads a user by his username.
*
* @param string $username
*
* @return User
*/
public function loadUserByUsername($username)
{
$user = UserQuery::create()->findOneByUsername($username);
@ -95,6 +166,13 @@ class UserProvider implements UserProviderInterface
return $user;
}
/**
* Refresh an user.
*
* @param User $user
*
* @return User
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof User) {
@ -104,6 +182,13 @@ class UserProvider implements UserProviderInterface
return $this->loadUserByUsername($user->getUsername());
}
/**
* Checks if the class is supported.
*
* @param string $class
*
* @return bool
*/
public function supportsClass($class)
{
return $class === 'Gist\Model\User';