Skip to content
This repository was archived by the owner on May 2, 2020. It is now read-only.

Commit 445aa3e

Browse files
author
Rick van Laarhoven
committed
Initial commit
1 parent a3bbabf commit 445aa3e

175 files changed

Lines changed: 92822 additions & 2 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.bowerrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"directory": "Resources/public/components",
3+
"analytics": false
4+
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
phpunit.xml
3+
vendor/

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
8+
env:
9+
- SYMFONY_DEPRECATIONS_HELPER=weak
10+
11+
before_install: composer selfupdate
12+
13+
install: composer update --prefer-dist --no-interaction
14+
15+
script: phpunit --coverage-text
16+
17+
branches:
18+
only:
19+
- master
20+
- develop

Command/CronRunCommand.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace Opifer\CmsBundle\Command;
4+
5+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Process\Process;
9+
use Symfony\Component\Process\ProcessBuilder;
10+
use Opifer\CmsBundle\Entity\Cron;
11+
12+
/**
13+
* This command is the main entry point for pending cronjobs
14+
*
15+
* To add cronjobs, simply create your own symfony command and add that command
16+
* to the cronjobs page in the cms.
17+
*
18+
* @see http://symfony.com/doc/current/cookbook/console/console_command.html
19+
*/
20+
class CronRunCommand extends ContainerAwareCommand
21+
{
22+
/** @var string */
23+
private $env;
24+
25+
/** @var ManagerRegistry */
26+
private $registry;
27+
28+
/** @var OutputInterface */
29+
private $output;
30+
31+
/**
32+
* {@inheritDoc}
33+
*/
34+
protected function configure()
35+
{
36+
$this
37+
->setName('opifer:cron:run')
38+
->setDescription('Runs all due cronjobs from the queue.');
39+
}
40+
41+
/**
42+
* Execute
43+
*
44+
* @param InputInterface $input
45+
* @param OutputInterface $output
46+
*
47+
* @return void
48+
*/
49+
protected function execute(InputInterface $input, OutputInterface $output)
50+
{
51+
$this->env = $input->getOption('env');
52+
$this->registry = $this->getContainer()->get('doctrine');
53+
$this->output = $output;
54+
55+
$this->runCrons();
56+
}
57+
58+
/**
59+
* Run jobs
60+
*
61+
* @return void
62+
*/
63+
private function runCrons()
64+
{
65+
$due = $this->getRepository()->findDue();
66+
67+
foreach ($due as $cron) {
68+
$this->startCron($cron);
69+
}
70+
71+
$this->output->writeln('All done.');
72+
}
73+
74+
/**
75+
* Start a cron
76+
*
77+
* @param Cron $cron
78+
*
79+
* @return void
80+
*/
81+
private function startCron(Cron $cron)
82+
{
83+
$this->output->writeln(sprintf('Started %s.', $cron));
84+
$this->changeState($cron, Cron::STATE_RUNNING);
85+
86+
$pb = $this->getCommandProcessBuilder();
87+
$pb->add($cron->getCommand());
88+
89+
$process = $pb->getProcess();
90+
$process->run(function ($type, $buffer) { // or ->start() to make processes run asynchronously
91+
if (Process::ERR === $type) {
92+
$this->output->writeln('ERR > '.$buffer);
93+
} else {
94+
$this->output->writeln(' > '.$buffer);
95+
}
96+
});
97+
98+
if (!$process->isSuccessful()) {
99+
$this->changeState($cron, Cron::STATE_FAILED);
100+
101+
throw new \RuntimeException($process->getErrorOutput());
102+
}
103+
104+
$this->changeState($cron, Cron::STATE_FINISHED);
105+
}
106+
107+
/**
108+
* Change the state of the cron
109+
*
110+
* @param Cron $cron
111+
* @param string $state
112+
*
113+
* @return void
114+
*/
115+
private function changeState(Cron $cron, $state)
116+
{
117+
$cron->setState($state);
118+
119+
$em = $this->getEntityManager();
120+
$em->persist($cron);
121+
$em->flush($cron);
122+
}
123+
124+
/**
125+
* Get the process builder
126+
*
127+
* @return \Symfony\Component\Process\ProcessBuilder
128+
*/
129+
private function getCommandProcessBuilder()
130+
{
131+
$pb = new ProcessBuilder();
132+
133+
// PHP wraps the process in "sh -c" by default, but we need to control
134+
// the process directly.
135+
if (! defined('PHP_WINDOWS_VERSION_MAJOR')) {
136+
$pb->add('exec');
137+
}
138+
139+
$pb
140+
->add('php')
141+
->add($this->getContainer()->getParameter('kernel.root_dir').'/console')
142+
->add('--env='.$this->env)
143+
;
144+
145+
return $pb;
146+
}
147+
148+
/**
149+
* Get the entity manager
150+
*
151+
* @return \Doctrine\ORM\EntityManager
152+
*/
153+
private function getEntityManager()
154+
{
155+
return $this->registry->getManagerForClass('OpiferCmsBundle:Cron');
156+
}
157+
158+
/**
159+
* Get repository
160+
*
161+
* @return \Doctrine\ORM\EntityRepository
162+
*/
163+
private function getRepository()
164+
{
165+
return $this->getEntityManager()->getRepository('OpiferCmsBundle:Cron');
166+
}
167+
}

Command/CronTestCommand.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Opifer\CmsBundle\Command;
4+
5+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class CronTestCommand extends ContainerAwareCommand
10+
{
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
protected function configure()
15+
{
16+
$this
17+
->setName('opifer:cron:test')
18+
->setDescription('Just some test command to see whether cronjobs are running')
19+
;
20+
}
21+
22+
/**
23+
* Execute
24+
*
25+
* @param InputInterface $input
26+
* @param OutputInterface $output
27+
*
28+
* @return void
29+
*/
30+
protected function execute(InputInterface $input, OutputInterface $output)
31+
{
32+
$output->writeln('Test command. Crons are running!');
33+
}
34+
}

Command/RefreshCommand.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Opifer\CmsBundle\Command;
4+
5+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Symfony\Component\Console\Input\ArrayInput;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
/**
11+
* Refresh Command
12+
*
13+
* A simple way to refresh the application in one command
14+
*
15+
* Usage from inside root:
16+
* app/console opifer:refresh
17+
*
18+
* To change the environment (default is dev):
19+
* app/console opifer:refresh --env=prod
20+
*
21+
* @see http://symfony.com/doc/current/cookbook/console/console_command.html
22+
* @see http://symfony.com/doc/current/components/console/introduction.html
23+
*/
24+
class RefreshCommand extends ContainerAwareCommand
25+
{
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
protected function configure()
30+
{
31+
$this
32+
->setName('opifer:refresh')
33+
->setDescription('Refreshes the app')
34+
;
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
*/
40+
protected function execute(InputInterface $input, OutputInterface $output)
41+
{
42+
$this->clearCache($input, $output);
43+
$this->installAssets($input, $output);
44+
$this->asseticDump($input, $output);
45+
}
46+
47+
/**
48+
* Clears the cache for the current environment
49+
*
50+
* @param InputInterface $input
51+
* @param OutputInterface $output
52+
*
53+
* @return void
54+
*/
55+
private function clearCache($input, $output)
56+
{
57+
$command = $this->getApplication()->find('cache:clear');
58+
59+
$newInput = new ArrayInput([
60+
'command' => 'cache:clear',
61+
'--env' => $input->getOption('env'),
62+
]);
63+
64+
$command->run($newInput, $output);
65+
}
66+
67+
/**
68+
* Installs all assets as symlinks
69+
*
70+
* @param InputInterface $input
71+
* @param OutputInterface $output
72+
*
73+
* @return void
74+
*/
75+
private function installAssets($input, $output)
76+
{
77+
$command = $this->getApplication()->find('assets:install');
78+
79+
$newInput = new ArrayInput([
80+
'command' => 'assets:install',
81+
'target' => 'web',
82+
'--symlink' => ($input->getOption('env') == 'dev') ? true : false,
83+
'--relative' => true,
84+
]);
85+
86+
$command->run($newInput, $output);
87+
}
88+
89+
/**
90+
* Dump the assets to the web directory
91+
*
92+
* @param InputInterface $input
93+
* @param OutputInterface $output
94+
*
95+
* @return void
96+
*/
97+
private function asseticDump($input, $output)
98+
{
99+
$command = $this->getApplication()->find('assetic:dump');
100+
101+
$newInput = new ArrayInput([
102+
'command' => 'assetic:dump',
103+
]);
104+
105+
$command->run($newInput, $output);
106+
}
107+
}

Controller/CKEditorController.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Opifer\CmsBundle\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\HttpFoundation\Request;
8+
9+
class CKEditorController extends Controller
10+
{
11+
/**
12+
* Content browser for CKEditor
13+
*
14+
* @param Request $request
15+
*
16+
* @return Response
17+
*/
18+
public function contentAction(Request $request)
19+
{
20+
return $this->render('OpiferCmsBundle:CKEditor:content.html.twig', [
21+
'funcNum' => $request->get('CKEditorFuncNum'),
22+
'CKEditor' => $request->get('CKEditor'),
23+
'type' => $request->get('type')
24+
]);
25+
}
26+
27+
/**
28+
* Image browser for CKEditor
29+
*
30+
* @param Request $request
31+
*
32+
* @return Response
33+
*/
34+
public function mediaAction(Request $request)
35+
{
36+
$providers = $this->get('opifer.media.provider.pool')->getProviders();
37+
38+
return $this->render('OpiferCmsBundle:CKEditor:media.html.twig', [
39+
'providers' => $providers,
40+
'funcNum' => $request->get('CKEditorFuncNum'),
41+
'CKEditor' => $request->get('CKEditor'),
42+
'type' => $request->get('type')
43+
]);
44+
}
45+
}

0 commit comments

Comments
 (0)