Advertisement
davekok

Make Symfony 3 project with Sonata Admin and FOS UserBundle

May 19th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 10.52 KB | None | 0 0
  1. # Make file to install symfony, sonata-admin-bundle and fos-user-bundle
  2.  
  3. # Usage:
  4. # make all app=<project-name>
  5. # make symfony app=<project-name>
  6. # make fos-user app=<project-name>
  7. # make sonata-admin app=<project-name>
  8.  
  9. # composer command
  10. composer=composer5.6
  11. # console command
  12. console=php5.6 bin/console
  13.  
  14. include $(app).make
  15.  
  16. all: entities | symfony fos-user sonata-admin
  17. symfony: $(app)
  18. sonata-admin: $(app)/vendor/sonata-project/admin-bundle
  19. fos-user: $(app)/vendor/friendsofsymfony/user-bundle
  20. entities: $(app)/var/schema-update.time
  21.  
  22. # clear cache
  23. clear: FORCE
  24.     cd $(app) && $(console) cache:clear
  25.  
  26.  
  27.  
  28. ## Symfony framework
  29.  
  30. # Target for symfony framework
  31. $(app):
  32. # Create project with symfony framework
  33.     $(composer) create-project symfony/framework-standard-edition $(app)
  34. # Set access for both you and the webserver to all access for logs, cache and sessions folders in var folder
  35.     setfacl -R -m u:www-data:rwx -m u:$(USER):rwx $(app)/var/logs $(app)/var/cache $(app)/var/sessions
  36. # Set default access for same folders, so files created in the future will get the correct access
  37.     setfacl -dR -m u:www-data:rwx -m u:$(USER):rwx $(app)/var/logs $(app)/var/cache $(app)/var/sessions
  38. # Change app_dev.php to allow white listed IP-addresses
  39. # Make needs you to escape $ sign so use $$ instead of $
  40.     sed -i 's/!(in_array(@\$$_SERVER\['\''REMOTE_ADDR'\''\], \['\''127\.0\.0\.1'\'', '\''fe80::1'\'', '\''::1'\''\])/!($$whiteIP/' $(app)/web/app_dev.php
  41. # Change app_dev.php to check if IP-address is white listed or in white listed range.
  42. # Make needs you to escape $ sign so use $$ instead of $
  43.     sed -i '/if (isset(\$$_SERVER\['\''HTTP_CLIENT_IP'\''\])/i\
  44. $$allowed = ["10."];\
  45. array_walk($$allowed,function($$allow)use(&$$whiteIP){if(strpos($$_SERVER["REMOTE_ADDR"],$$allow)===0)$$whiteIP=true;});\
  46. ' $(app)/web/app_dev.php
  47. # Use postgresql instead of mysql
  48.     sed -i 's/pdo_mysql/pdo_pgsql/' $(app)/app/config/config.yml
  49. # Activate translator
  50.     sed -i 's/#translator/translator/' $(app)/app/config/config.yml
  51.  
  52.  
  53.  
  54. ## Sonata Admin Bundle
  55.  
  56. # append to app/config/config.yml
  57. define sonata_admin_config_yml
  58.  
  59. sonata_block:
  60.     default_contexts: [cms]
  61.     blocks:
  62.         # Enable the SonataAdminBundle block
  63.         sonata.admin.block.admin_list:
  64.             contexts:   [admin]
  65. endef
  66. export sonata_admin_config_yml
  67.  
  68. # append to app/config/routing.yml
  69. define sonata_admin_routing_yml
  70.  
  71. admin:
  72.     resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
  73.     prefix: /admin
  74.  
  75. _sonata_admin:
  76.     resource: .
  77.     type: sonata_admin
  78.     prefix: /admin
  79. endef
  80. export sonata_admin_routing_yml
  81.  
  82. # Target for sonata admin: requires symfony
  83. $(app)/vendor/sonata-project/admin-bundle: | $(app)
  84. # Require dependencies
  85.     cd $(app) && $(composer) require sonata-project/admin-bundle --no-update
  86.     cd $(app) && $(composer) require sonata-project/doctrine-orm-admin-bundle --no-update
  87.     cd $(app) && $(composer) update
  88. # Inject dependencies in AppKernel
  89.     sed -i '/new AppBundle\\AppBundle()/i\
  90.            new Sonata\\CoreBundle\\SonataCoreBundle(),\
  91.            new Sonata\\BlockBundle\\SonataBlockBundle(),\
  92.            new Knp\\Bundle\\MenuBundle\\KnpMenuBundle(),\
  93.            new Sonata\\DoctrineORMAdminBundle\\SonataDoctrineORMAdminBundle(),\
  94.            new Sonata\\AdminBundle\\SonataAdminBundle(),' $(app)/app/AppKernel.php
  95. # Enable sonata block in config.yml
  96. # Make needs you to escape $ sign so use $$ instead of $
  97.     echo "$$sonata_admin_config_yml" >> $(app)/app/config/config.yml
  98. # Add routes in routing.yml
  99. # Make needs you to escape $ sign so use $$ instead of $
  100.     echo "$$sonata_admin_routing_yml" >> $(app)/app/config/routing.yml
  101. # Install assests
  102.     cd $(app) && $(console) assets:install web
  103. # Clear cache
  104.     cd $(app) && $(console) cache:clear
  105.  
  106.  
  107.  
  108. ## FOS User Bundle
  109.  
  110. # Append to app/config/config.yml
  111. define fos_user_config_yml
  112.  
  113. fos_user:
  114.     db_driver: orm
  115.     firewall_name: main
  116.     user_class: AppBundle\Entity\User
  117. endef
  118. export fos_user_config_yml
  119.  
  120. # Append to app/config/routing.yml
  121. define fos_user_routing_yml
  122.  
  123. fos_user:
  124.     resource: "@FOSUserBundle/Resources/config/routing/all.xml"
  125. endef
  126. export fos_user_routing_yml
  127.  
  128. # app/config/security.yml
  129. # Make needs you to escape $ sign so use $$ instead of $
  130. define fos_user_security_yml
  131. security:
  132.     encoders:
  133.         FOS\UserBundle\Model\UserInterface: bcrypt
  134.  
  135.     role_hierarchy:
  136.         ROLE_ADMIN:       ROLE_USER
  137.         ROLE_SUPER_ADMIN: ROLE_ADMIN ROLE_SONATA_ADMIN
  138.  
  139.     providers:
  140.         fos_userbundle:
  141.             id: fos_user.user_provider.username
  142.  
  143.     firewalls:
  144.         main:
  145.             pattern: ^/
  146.             form_login:
  147.                 provider: fos_userbundle
  148.                 csrf_token_generator: security.csrf.token_manager
  149.             logout:       true
  150.             anonymous:    true
  151.  
  152.     access_control:
  153.         - { path: ^/login$$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  154.         - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
  155.         - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
  156.         - { path: ^/admin/, role: ROLE_ADMIN }
  157. endef
  158. export fos_user_security_yml
  159.  
  160. # src/AppBundle/Entity/User.php
  161. # Make needs you to escape $ sign so use $$ instead of $
  162. define fos_user_user_php
  163. <?php
  164. namespace AppBundle\Entity;
  165.  
  166. use FOS\UserBundle\Model\User as BaseUser;
  167. use Doctrine\ORM\Mapping as ORM;
  168.  
  169. /**
  170.  * @ORM\Entity
  171.  * @ORM\Table(name="fos_user")
  172.  */
  173. class User extends BaseUser
  174. {
  175.     /**
  176.      * @ORM\Id
  177.      * @ORM\Column(type="integer")
  178.      * @ORM\GeneratedValue(strategy="AUTO")
  179.      */
  180.     protected $$id;
  181.  
  182.     private $$newPass;
  183.  
  184.     public function setNewPass($$newPass) {
  185.         $$this->newPass = $$newPass;
  186.     }
  187.  
  188.     public function getNewPass() {
  189.         return $$this->newPass;
  190.     }
  191. }
  192. endef
  193. export fos_user_user_php
  194.  
  195. # src/AppBundle/Entity/User.php
  196. # Make needs you to escape $ sign so use $$ instead of $
  197. define fos_user_admin_user_php
  198. <?php
  199.  
  200. namespace AppBundle\Admin;
  201.  
  202. use Sonata\AdminBundle\Admin\AbstractAdmin;
  203. use Sonata\AdminBundle\Datagrid\ListMapper;
  204. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  205. use Sonata\AdminBundle\Form\FormMapper;
  206.  
  207. class UserAdmin extends AbstractAdmin
  208. {
  209.     public function prePersist($object) {
  210.         parent::prePersist($object);
  211.         $$this->updateUser($$object);
  212.     }
  213.  
  214.     public function preUpdate($$object) {
  215.         parent::preUpdate($$object);
  216.         $$this->updateUser($$object);
  217.     }
  218.  
  219.     public function updateUser(\AppBundle\Entity\User $$u) {
  220.         if ($$u->getNewPass()) {
  221.             $$u->setPlainPassword($$u->getNewPass());
  222.         }
  223.  
  224.         $$um = $$this->getConfigurationPool()->getContainer()->get('fos_user.user_manager');
  225.         $$um->updateUser($$u, false);
  226.     }
  227.  
  228.     protected function configureFormFields(FormMapper $$formMapper)
  229.     {
  230.         $$formMapper
  231.             ->add('username', 'text')
  232.             ->add('email', 'text')
  233.             ->add('newPass', 'text', array(
  234.                 'label' => 'New password (empty filed means no changes)',
  235.                 'required' => FALSE
  236.             ))
  237.         ;
  238.     }
  239.  
  240.     protected function configureDatagridFilters(DatagridMapper $$datagridMapper)
  241.     {
  242.         $$datagridMapper->add('username');
  243.         $$datagridMapper->add('email');
  244.     }
  245.  
  246.     protected function configureListFields(ListMapper $$listMapper)
  247.     {
  248.         $$listMapper->addIdentifier('username');
  249.         $$listMapper->addIdentifier('email');
  250.     }
  251. }
  252. endef
  253. export fos_user_admin_user_php
  254.  
  255. # Target for fos user bundle: requires symfony
  256. $(app)/vendor/friendsofsymfony/user-bundle: | $(app)
  257. # Require dependencies
  258.     cd $(app) && $(composer) require doctrine/orm --no-update
  259.     cd $(app) && $(composer) require friendsofsymfony/user-bundle "dev-master"  --no-update
  260.     cd $(app) && $(composer) update
  261. # Inject dependencies in AppKernel
  262.     sed -i '/new AppBundle\\AppBundle()/i\
  263.             new FOS\\UserBundle\\FOSUserBundle(),' $(app)/app/AppKernel.php
  264. # Configure fos user-bundle in config.yml
  265. # Make needs you to escape $ sign so use $$ instead of $
  266.     echo "$$fos_user_config_yml" >> $(app)/app/config/config.yml
  267. # Add routes in routing.yml
  268. # Make needs you to escape $ sign so use $$ instead of $
  269.     echo "$$fos_user_routing_yml" >> $(app)/app/config/routing.yml
  270. # Override secrity.yml
  271. # Make needs you to escape $ sign so use $$ instead of $
  272.     echo "$$fos_user_security_yml" > $(app)/app/config/security.yml
  273. # Create Entity folder
  274.     mkdir -p $(app)/src/AppBundle/Entity
  275. # Create user entity
  276. # Make needs you to escape $ sign so use $$ instead of $
  277.     echo "$$fos_user_user_php" > $(app)/src/AppBundle/Entity/User.php
  278. # Create Admin folder
  279.     mkdir -p $(app)/src/AppBundle/Admin
  280. # Create admin user
  281. # Make needs you to escape $ sign so use $$ instead of $
  282.     echo "$$fos_user_admin_user_php" > $(app)/src/AppBundle/Admin/UserAdmin.php
  283. # Update schema
  284.     cd $(app) && $(console) doctrine:schema:update --force
  285. # Create first user
  286. # Assuming you have your git configuration done
  287. # Don't forget to change your password
  288.     cd $(app) && $(console) fos:user:create $(USER) `git config --global user.email` admin --super-admin
  289. # Clear cache
  290.     cd $(app) && $(console) cache:clear
  291.  
  292.  
  293.  
  294.  
  295.  
  296. ## Generate entities and update schema
  297.  
  298. # Basic entity
  299. define entity_php
  300. <?php
  301.  
  302. namespace AppBundle\Entity;
  303.  
  304. use Doctrine\ORM\Mapping as ORM;
  305.  
  306. /**
  307.  * $*
  308.  *
  309.  * @ORM\Table()
  310.  * @ORM\Entity(repositoryClass="AppBundle\Repository\$*Repository")
  311.  */
  312. class $*
  313. {
  314.     /**
  315.      * @var int
  316.      *
  317.      * @ORM\Column(type="integer")
  318.      * @ORM\Id
  319.      * @ORM\GeneratedValue(strategy="AUTO")
  320.      */
  321.     private $$id;
  322. }
  323. endef
  324. export entity_php
  325.  
  326. # Basic entity repository
  327. define repository_php
  328. <?php
  329.  
  330. namespace AppBundle\Repository;
  331.  
  332. /**
  333.  * $*Repository
  334.  *
  335.  * This class was generated by the Doctrine ORM. Add your own custom
  336.  * repository methods below.
  337.  */
  338. class $*Repository extends \Doctrine\ORM\EntityRepository
  339. {
  340. }
  341. endef
  342. export repository_php
  343.  
  344. $(app)/var/schema-update.time: $(patsubst %,$(app)/src/AppBundle/Entity/%.php~,$(entities))
  345.     cd $(app) && $(console) doctrine:schema:update --force
  346.     touch $@
  347.  
  348. $(app)/src/AppBundle/Entity/%.php~: $(app)/src/AppBundle/Entity/%.php | $(app)/src/AppBundle/Repository/%Repository.php
  349.     cd $(app) && $(console) doctrine:generate:entities AppBundle/Entity/$*
  350.  
  351. .PRECIOUS: $(app)/src/AppBundle/Entity/%.php
  352. $(app)/src/AppBundle/Entity/%.php:
  353.     echo "$$entity_php" > $@
  354.  
  355. .PRECIOUS: $(app)/src/AppBundle/Repository/%Repository.php
  356. $(app)/src/AppBundle/Repository/%Repository.php:
  357.     echo "$$repository_php" > $@
  358.  
  359.  
  360.  
  361.  
  362. .PHONY: FORCE
  363. FORCE:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement