davekok icon

Make Symfony 3 project with Sonata Admin and FOS UserBundle

davekok | PRO | 05/19/16 07:55:08 AM UTC | 0 ⭐ | 7620 👁️ | Never ⏰ | []
Make |

10.52 KB

|

None

|

0 👍

/

0 👎

# Make file to install symfony, sonata-admin-bundle and fos-user-bundle
 
# Usage:
# make all app=<project-name>
# make symfony app=<project-name>
# make fos-user app=<project-name>
# make sonata-admin app=<project-name>
 
# composer command
composer=composer5.6
# console command
console=php5.6 bin/console
 
include $(app).make
 
all: entities | symfony fos-user sonata-admin
symfony: $(app)
sonata-admin: $(app)/vendor/sonata-project/admin-bundle
fos-user: $(app)/vendor/friendsofsymfony/user-bundle
entities: $(app)/var/schema-update.time
 
# clear cache
clear: FORCE
    cd $(app) && $(console) cache:clear
 
 
 
## Symfony framework
 
# Target for symfony framework
$(app):
# Create project with symfony framework
    $(composer) create-project symfony/framework-standard-edition $(app)
# Set access for both you and the webserver to all access for logs, cache and sessions folders in var folder
    setfacl -R -m u:www-data:rwx -m u:$(USER):rwx $(app)/var/logs $(app)/var/cache $(app)/var/sessions
# Set default access for same folders, so files created in the future will get the correct access
    setfacl -dR -m u:www-data:rwx -m u:$(USER):rwx $(app)/var/logs $(app)/var/cache $(app)/var/sessions
# Change app_dev.php to allow white listed IP-addresses
# Make needs you to escape $ sign so use $$ instead of $
    sed -i 's/!(in_array(@\$$_SERVER\['\''REMOTE_ADDR'\''\], \['\''127\.0\.0\.1'\'', '\''fe80::1'\'', '\''::1'\''\])/!($$whiteIP/' $(app)/web/app_dev.php
# Change app_dev.php to check if IP-address is white listed or in white listed range.
# Make needs you to escape $ sign so use $$ instead of $
    sed -i '/if (isset(\$$_SERVER\['\''HTTP_CLIENT_IP'\''\])/i\
$$allowed = ["10."];\
array_walk($$allowed,function($$allow)use(&$$whiteIP){if(strpos($$_SERVER["REMOTE_ADDR"],$$allow)===0)$$whiteIP=true;});\
' $(app)/web/app_dev.php
# Use postgresql instead of mysql
    sed -i 's/pdo_mysql/pdo_pgsql/' $(app)/app/config/config.yml
# Activate translator
    sed -i 's/#translator/translator/' $(app)/app/config/config.yml
 
 
 
## Sonata Admin Bundle
 
# append to app/config/config.yml
define sonata_admin_config_yml
 
sonata_block:
    default_contexts: [cms]
    blocks:
        # Enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]
endef
export sonata_admin_config_yml
 
# append to app/config/routing.yml
define sonata_admin_routing_yml
 
admin:
    resource: "@SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
    prefix: /admin
 
_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin
endef
export sonata_admin_routing_yml
 
# Target for sonata admin: requires symfony
$(app)/vendor/sonata-project/admin-bundle: | $(app)
# Require dependencies
    cd $(app) && $(composer) require sonata-project/admin-bundle --no-update
    cd $(app) && $(composer) require sonata-project/doctrine-orm-admin-bundle --no-update
    cd $(app) && $(composer) update
# Inject dependencies in AppKernel
    sed -i '/new AppBundle\\AppBundle()/i\
            new Sonata\\CoreBundle\\SonataCoreBundle(),\
            new Sonata\\BlockBundle\\SonataBlockBundle(),\
            new Knp\\Bundle\\MenuBundle\\KnpMenuBundle(),\
            new Sonata\\DoctrineORMAdminBundle\\SonataDoctrineORMAdminBundle(),\
            new Sonata\\AdminBundle\\SonataAdminBundle(),' $(app)/app/AppKernel.php
# Enable sonata block in config.yml
# Make needs you to escape $ sign so use $$ instead of $
    echo "$$sonata_admin_config_yml" >> $(app)/app/config/config.yml
# Add routes in routing.yml
# Make needs you to escape $ sign so use $$ instead of $
    echo "$$sonata_admin_routing_yml" >> $(app)/app/config/routing.yml
# Install assests
    cd $(app) && $(console) assets:install web
# Clear cache
    cd $(app) && $(console) cache:clear
 
 
 
## FOS User Bundle
 
# Append to app/config/config.yml
define fos_user_config_yml
 
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: AppBundle\Entity\User
endef
export fos_user_config_yml
 
# Append to app/config/routing.yml
define fos_user_routing_yml
 
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"
endef
export fos_user_routing_yml
 
# app/config/security.yml
# Make needs you to escape $ sign so use $$ instead of $
define fos_user_security_yml
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt
 
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN ROLE_SONATA_ADMIN
 
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username
 
    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
            logout:       true
            anonymous:    true
 
    access_control:
        - { path: ^/login$$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }
endef
export fos_user_security_yml
 
# src/AppBundle/Entity/User.php
# Make needs you to escape $ sign so use $$ instead of $
define fos_user_user_php
<?php
namespace AppBundle\Entity;
 
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
 
/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $$id;
 
    private $$newPass;
 
    public function setNewPass($$newPass) {
        $$this->newPass = $$newPass;
    }
 
    public function getNewPass() {
        return $$this->newPass;
    }
}
endef
export fos_user_user_php
 
# src/AppBundle/Entity/User.php
# Make needs you to escape $ sign so use $$ instead of $
define fos_user_admin_user_php
<?php
 
namespace AppBundle\Admin;
 
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
 
class UserAdmin extends AbstractAdmin
{
    public function prePersist($object) {
        parent::prePersist($object);
        $$this->updateUser($$object);
    }
 
    public function preUpdate($$object) {
        parent::preUpdate($$object);
        $$this->updateUser($$object);
    }
 
    public function updateUser(\AppBundle\Entity\User $$u) {
        if ($$u->getNewPass()) {
            $$u->setPlainPassword($$u->getNewPass());
        }
 
        $$um = $$this->getConfigurationPool()->getContainer()->get('fos_user.user_manager');
        $$um->updateUser($$u, false);
    }
 
    protected function configureFormFields(FormMapper $$formMapper)
    {
        $$formMapper
            ->add('username', 'text')
            ->add('email', 'text')
            ->add('newPass', 'text', array(
                'label' => 'New password (empty filed means no changes)',
                'required' => FALSE
            ))
        ;
    }
 
    protected function configureDatagridFilters(DatagridMapper $$datagridMapper)
    {
        $$datagridMapper->add('username');
        $$datagridMapper->add('email');
    }
 
    protected function configureListFields(ListMapper $$listMapper)
    {
        $$listMapper->addIdentifier('username');
        $$listMapper->addIdentifier('email');
    }
}
endef
export fos_user_admin_user_php
 
# Target for fos user bundle: requires symfony
$(app)/vendor/friendsofsymfony/user-bundle: | $(app)
# Require dependencies
    cd $(app) && $(composer) require doctrine/orm --no-update
    cd $(app) && $(composer) require friendsofsymfony/user-bundle "dev-master"  --no-update
    cd $(app) && $(composer) update
# Inject dependencies in AppKernel
    sed -i '/new AppBundle\\AppBundle()/i\
             new FOS\\UserBundle\\FOSUserBundle(),' $(app)/app/AppKernel.php
# Configure fos user-bundle in config.yml
# Make needs you to escape $ sign so use $$ instead of $
    echo "$$fos_user_config_yml" >> $(app)/app/config/config.yml
# Add routes in routing.yml
# Make needs you to escape $ sign so use $$ instead of $
    echo "$$fos_user_routing_yml" >> $(app)/app/config/routing.yml
# Override secrity.yml
# Make needs you to escape $ sign so use $$ instead of $
    echo "$$fos_user_security_yml" > $(app)/app/config/security.yml
# Create Entity folder
    mkdir -p $(app)/src/AppBundle/Entity
# Create user entity
# Make needs you to escape $ sign so use $$ instead of $
    echo "$$fos_user_user_php" > $(app)/src/AppBundle/Entity/User.php
# Create Admin folder
    mkdir -p $(app)/src/AppBundle/Admin
# Create admin user
# Make needs you to escape $ sign so use $$ instead of $
    echo "$$fos_user_admin_user_php" > $(app)/src/AppBundle/Admin/UserAdmin.php
# Update schema
    cd $(app) && $(console) doctrine:schema:update --force
# Create first user
# Assuming you have your git configuration done
# Don't forget to change your password
    cd $(app) && $(console) fos:user:create $(USER) `git config --global user.email` admin --super-admin
# Clear cache
    cd $(app) && $(console) cache:clear
 
 
 
 
 
## Generate entities and update schema
 
# Basic entity
define entity_php
<?php
 
namespace AppBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
 
/**
 * $*
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Repository\$*Repository")
 */
class $*
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $$id;
}
endef
export entity_php
 
# Basic entity repository
define repository_php
<?php
 
namespace AppBundle\Repository;
 
/**
 * $*Repository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class $*Repository extends \Doctrine\ORM\EntityRepository
{
}
endef
export repository_php
 
$(app)/var/schema-update.time: $(patsubst %,$(app)/src/AppBundle/Entity/%.php~,$(entities))
    cd $(app) && $(console) doctrine:schema:update --force
    touch $@
 
$(app)/src/AppBundle/Entity/%.php~: $(app)/src/AppBundle/Entity/%.php | $(app)/src/AppBundle/Repository/%Repository.php
    cd $(app) && $(console) doctrine:generate:entities AppBundle/Entity/$*
 
.PRECIOUS: $(app)/src/AppBundle/Entity/%.php
$(app)/src/AppBundle/Entity/%.php:
    echo "$$entity_php" > $@
 
.PRECIOUS: $(app)/src/AppBundle/Repository/%Repository.php
$(app)/src/AppBundle/Repository/%Repository.php:
    echo "$$repository_php" > $@
 
 
 
 
.PHONY: FORCE
FORCE:

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎